source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/Subroutine.cpp@ 523

Last change on this file since 523 was 523, checked in by dai_9181, 16 years ago

ヘッダファイルを整理中

File size: 11.8 KB
RevLine 
[206]1#include "stdafx.h"
2
[193]3#include <Compiler.h>
[206]4#include <Procedure.h>
[182]5
[4]6#include "../BasicCompiler_Common/common.h"
7
8#ifdef _AMD64_
[485]9#include "../compiler_x64/opcode.h"
[4]10#else
[484]11#include "../compiler_x86/opcode.h"
[4]12#endif
13
14int GetCallProcName(char *buffer,char *name){
15 int i2,i3,IsStr=0;
16
17 for(i2=0;;i2++){
18 if(buffer[i2]=='\"') IsStr^=1;
19 if(IsDBCSLeadByte(buffer[i2])){
20 name[i2]=buffer[i2];
21 i2++;
22 name[i2]=buffer[i2];
23 continue;
24 }
25 if(buffer[i2]=='['&&IsStr==0){
26 i3=GetStringInBracket(name+i2,buffer+i2);
27 i2+=i3-1;
28 continue;
29 }
30 if(buffer[i2]=='('&&IsStr==0){
31 name[i2]=0;
32 break;
33 }
34 if(buffer[i2]=='='&&IsStr==0){
35 name[i2]=0;
36 break;
37 }
38
39 name[i2]=buffer[i2];
40 if(buffer[i2]=='\0') break;
41 }
42 return i2;
43}
44
45int GetProc(char *name,void **ppInfo){
46
47 //ユーザー定義関数
48 *ppInfo=(void *)GetSubHash(name);
49 if(*ppInfo) return PROC_DEFAULT;
50
51 //DLL関数
52 *ppInfo=(void *)GetDeclareHash(name);
53 if(*ppInfo) return PROC_DLL;
54
55 //コンパイラ埋め込み型
56 *ppInfo=(void *)(_int64)GetFunctionFromName(name);
57 if(*ppInfo) return PROC_BUILTIN;
58
[327]59
60 /////////////////////////////////////////////////////////////////
61 //関数ポインタ、またはデリゲート
62 /////////////////////////////////////////////////////////////////
63
[75]64 Type type;
65 if( !GetVarType( name, type, false ) ){
66 return 0;
67 }
[327]68
69 if( type.IsProcPtr() )
70 {
71 // 関数ポインタ
[75]72 return PROC_PTR;
73 }
[4]74
[332]75 if( type.IsDelegate() )
[327]76 {
77 // デリゲート
78 return PROC_DELEGATE;
79 }
80
[4]81 return 0;
82}
83
[290]84void SplitObjectName(const char *name,char *ObjectName, ReferenceKind &referenceFind )
85{
86 referenceFind = RefNon;
87
[4]88 int i4;
89 for(i4=lstrlen(name)-1;i4>=0;i4--){
90 if(name[i4]=='.'||(name[i4]==1&&name[i4+1]==ESC_PSMEM))
91 break;
92 }
93 if(i4==-1) ObjectName[0]=0;
94 else{
95 //参照タイプを判別
[290]96 if(name[i4]=='.')
97 {
98 referenceFind = RefDot;
99 }
100 else
101 {
102 referenceFind = RefPointer;
103 }
[4]104
105 if(i4==0) GetWithName(ObjectName);
106 else{
107 memcpy(ObjectName,name,i4);
108 ObjectName[i4]=0;
109 }
110 }
111}
[97]112
[331]113bool CallProc( int kind, const void *pProc, const char *fullCallName, const char *lpszParms, const Type &baseType, Type &resultType, bool isCallOn )
114{
[75]115 //GetSubHash内でエラー提示が行われた場合
116 if(pProc==(Procedure *)-1){
117 return false;
118 }
[50]119
[75]120 if(kind==PROC_DEFAULT){
[50]121 /////////////////////
122 // ユーザー定義関数
123 /////////////////////
124
[206]125 const UserProc *pUserProc = (const UserProc *)pProc;
[50]126
127 //オブジェクト名を取得
128 char ObjectName[VN_SIZE];
[290]129 ReferenceKind referenceKind;
130 SplitObjectName(fullCallName,ObjectName, referenceKind );
[50]131
132
133 ////////////////////////
134 // オーバーロードを解決
135 ////////////////////////
136
[206]137 std::vector<const UserProc *> subs;
[75]138 GetOverloadSubHash(fullCallName,subs);
[50]139 if(subs.size()){
140 //オーバーロードを解決
[75]141 pUserProc=OverloadSolutionWithStrParam(fullCallName,subs,lpszParms,ObjectName);
[50]142
[75]143 if(!pUserProc){
144 return false;
145 }
[50]146 }
147
[75]148 resultType = pUserProc->ReturnType();
[50]149
[75]150 if( isCallOn ){
[290]151 if( !Opcode_CallProc(lpszParms,pUserProc,0,ObjectName ) ){
[75]152 return false;
153 }
[50]154 }
155 }
[75]156 else if(kind==PROC_DLL){
[50]157 /////////////////////////
158 // DLL関数
159 /////////////////////////
[75]160 DllProc *pDllProc = (DllProc *)pProc;
[50]161
[75]162 resultType = pDllProc->ReturnType();
163
164 if( isCallOn ){
165 if( !Opcode_CallDllProc(lpszParms,pDllProc) ){
166 return false;
167 }
168 }
[50]169 }
[75]170 else if(kind==PROC_BUILTIN){
[50]171 /////////////////////////
172 // 組み込み関数
173 /////////////////////////
[75]174 int FuncId = (int)(_int64)pProc;
[50]175
[331]176 if( !Opcode_CallFunc( lpszParms, FuncId, baseType, resultType, isCallOn ) ){
[75]177 return false;
[50]178 }
179 }
[75]180 else if(kind==PROC_PTR){
[50]181 /////////////////
182 // 関数ポインタ
183 /////////////////
184
[75]185 Type type;
186 GetVarType(fullCallName,type,false);
[50]187
[265]188 ProcPointer *pProcPtr = compiler.GetObjectModule().meta.GetProcPointers()[type.GetIndex()];
[75]189 resultType = pProcPtr->ReturnType();
190
191 if( isCallOn ){
192 if( !Opcode_CallProcPtr(fullCallName,lpszParms,pProcPtr) ){
193 return false;
194 }
195 }
[50]196 }
[327]197 else if( kind == PROC_DELEGATE )
198 {
199 // デリゲート
200 char tempName[VN_SIZE];
201 lstrcpy( tempName, fullCallName );
202 lstrcat( tempName, ".Call" );
203
204 void *pInfo=(void *)GetSubHash( tempName );
205 if( !pInfo )
206 {
207 Jenga::Throw( "デリゲートの内部Callメソッドの取得に失敗" );
208 }
209
[331]210 return CallProc( PROC_DEFAULT, pInfo, tempName, lpszParms, baseType, resultType, isCallOn );
[327]211 }
[75]212 else{
213 return false;
214 }
[50]215
[75]216 return true;
[50]217}
[75]218bool CallPropertyMethod( const char *variable, const char *rightSide, Type &resultType){
[50]219 //プロパティ用のメソッドを呼び出す
220
221 //配列要素を取得
222 char VarName[VN_SIZE],ArrayElements[VN_SIZE];
223 GetArrayElement(variable,VarName,ArrayElements);
224
225 //オブジェクト名を取得
226 char ObjectName[VN_SIZE];
[290]227 ReferenceKind referenceKind;
228 SplitObjectName(VarName,ObjectName, referenceKind );
[50]229
230 //オーバーロード用の関数リストを作成
[206]231 std::vector<const UserProc *> subs;
[50]232 GetOverloadSubHash(VarName,subs);
233 if(subs.size()==0){
[75]234 return false;
[50]235 }
236
237 //パラメータを整備
238 char *Parameter;
[75]239 Parameter=(char *)HeapAlloc(hHeap,0,lstrlen(ArrayElements)+lstrlen(rightSide)+32);
[50]240 lstrcpy(Parameter,ArrayElements);
[75]241 if(rightSide){
242 if(Parameter[0]&&rightSide[0]) lstrcat(Parameter,",");
243 lstrcat(Parameter,rightSide);
[50]244 }
245
246 //オーバーロードを解決
[206]247 const UserProc *pUserProc = OverloadSolutionWithStrParam(VarName,subs,Parameter,ObjectName);
[50]248
[75]249 if(pUserProc){
[50]250 //呼び出し
[290]251 Opcode_CallProc(Parameter,pUserProc,0,ObjectName);
[50]252
[75]253 resultType = pUserProc->ReturnType();
[292]254
255 Type leftType;
256 GetVarType( ObjectName, leftType, false );
257
[299]258 // 型パラメータを解決
259 ResolveFormalGenericTypeParameter( resultType, leftType, pUserProc );
[50]260 }
261
262 HeapDefaultFree(Parameter);
263
[75]264 return true;
[50]265}
266
[75]267bool GetReturnTypeOfPropertyMethod( const char *variable, const char *rightSide, Type &resultType ){
[4]268 //プロパティ用のメソッドを呼び出す
269
270 //配列要素を取得
271 char VarName[VN_SIZE],ArrayElements[VN_SIZE];
272 GetArrayElement(variable,VarName,ArrayElements);
273
274 //オブジェクト名を取得
275 char ObjectName[VN_SIZE];
[290]276 ReferenceKind referenceKind;
277 SplitObjectName(VarName,ObjectName, referenceKind );
[4]278
279 //オーバーロード用の関数リストを作成
[206]280 std::vector<const UserProc *> subs;
[50]281 GetOverloadSubHash(VarName,subs);
282 if(subs.size()==0){
[4]283 return 0;
284 }
285
286 //パラメータを整備
287 char *Parameter;
[75]288 Parameter=(char *)HeapAlloc(hHeap,0,lstrlen(ArrayElements)+lstrlen(rightSide)+32);
[4]289 lstrcpy(Parameter,ArrayElements);
[75]290 if(rightSide){
291 if(Parameter[0]&&rightSide[0]) lstrcat(Parameter,",");
292 lstrcat(Parameter,rightSide);
[4]293 }
294
295 //オーバーロードを解決
[206]296 const UserProc *pUserProc = OverloadSolutionWithStrParam(VarName,subs,Parameter,ObjectName);
[4]297
[75]298 if(pUserProc){
299 resultType = pUserProc->ReturnType();
[4]300 }
301
302 return 1;
303}
304
[38]305//インデクサ(getter)の戻り値を取得
[292]306bool GetReturnTypeOfIndexerGetterProc( const Type &classType, Type &resultType )
307{
[523]308 std::vector<const UserProc *> subs;
[342]309 classType.GetClass().GetDynamicMethods().Enum( CALC_ARRAY_GET, subs );
[50]310 if( subs.size() == 0 ){
[38]311 return false;
312 }
313
[299]314 const UserProc *pUserProc = subs[0];
[38]315
[299]316 resultType = pUserProc->ReturnType();
[292]317
318
[299]319 // 型パラメータを解決
320 ResolveFormalGenericTypeParameter( resultType, classType, pUserProc );
[292]321
[38]322 return true;
323}
324
[94]325bool IsNeedProcCompile(){
[265]326 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
327 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
[206]328 {
[265]329 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
[206]330 if( pUserProc->IsUsing() && pUserProc->IsCompiled() == false ){
331 return true;
[94]332 }
333 }
334 return false;
335}
[316]336
337void CompileBufferInProcedure( const UserProc &userProc ){
338 if( userProc.IsCompiled() ) return;
339
340 _compile_proc( &userProc );
341
342/*
343 // ログを履く
344 char temporary[8192];
345 temporary[0]=0;
346 lstrcat( temporary, "------------------------------------------------------------------\n" );
347 sprintf( temporary + lstrlen(temporary), "【 %s のコード情報】\n", userProc.GetFullName().c_str() );
348 sprintf( temporary + lstrlen(temporary), "code size: %d bytes\n", userProc.GetCodeSize() );
349 lstrcat( temporary, "------------------------------------------------------------------\n" );
350 lstrcat( temporary, "\n" );
351 Smoothie::Logger::Put( temporary );*/
352}
353void CompileLocal(){
354 if( compiler.IsDll() )
355 {
356 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
357 const UserProc *pUserProc=GetSubHash("_System_InitDllGlobalVariables");
358 if(pUserProc){
359 CompileBufferInProcedure( *pUserProc );
360 }
[465]361 else compiler.errorMessenger.Output(300,NULL,cp);
[316]362 }
363 else
364 {
365 // グローバル領域を一番初めにコンパイルする
366 extern const UserProc *pSub_System_GlobalArea;
367 CompileBufferInProcedure( *pSub_System_GlobalArea );
368 }
369
370 //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
371 extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
372 pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
373
[355]374 //_System_TypeBase_InitializeUserTypesForBaseTypeは一番最後にコンパイル
375 extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType;
376 pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType->CompleteCompile();
377
[316]378 //_System_InitStaticLocalVariablesは一番最後にコンパイル
379 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
380 extern const UserProc *pSub_System_InitStaticLocalVariables;
381 pSub_System_InitStaticLocalVariables->CompleteCompile();
382
383 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
384 extern const UserProc *pSub_System_Call_Destructor_of_GlobalObject;
385 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
386
[319]387 // _System_CGarbageCollection.RegisterGlobalRootsは一番最後にコンパイル
388 extern const UserProc *pUserProc_System_CGarbageCollection_RegisterGlobalRoots;
389 pUserProc_System_CGarbageCollection_RegisterGlobalRoots->CompleteCompile();
390
[316]391repeat:
392 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
393 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
394 {
395 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
396 CompileBufferInProcedure( *pUserProc );
397 }
398
399 if( IsNeedProcCompile() ){
400 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
401 goto repeat;
402 }
403
404 if( !compiler.IsStaticLibrary() )
405 {
406 //_System_TypeBase_InitializeUserTypesは最後のほうでコンパイル
407 pSubStaticMethod_System_TypeBase_InitializeUserTypes->KillCompileStatus();
408 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypes );
409
[355]410 //_System_TypeBase_InitializeUserTypesForBaseTypeは最後のほうでコンパイル
411 pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType->KillCompileStatus();
412 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType );
413
[316]414 if( IsNeedProcCompile() ){
415 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
416
417 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
418 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
419 {
420 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
421 CompileBufferInProcedure( *pUserProc );
422 }
423 }
424
425 //_System_InitStaticLocalVariablesは一番最後にコンパイル
426 pSub_System_InitStaticLocalVariables->KillCompileStatus();
427 CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
428
429 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
430 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
431 CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
[319]432
433 // _System_CGarbageCollection.RegisterGlobalRootsは一番最後にコンパイル
434 pUserProc_System_CGarbageCollection_RegisterGlobalRoots->KillCompileStatus();
435 CompileBufferInProcedure( *pUserProc_System_CGarbageCollection_RegisterGlobalRoots );
[316]436 }
437}
Note: See TracBrowser for help on using the repository browser.