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

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

NamespaceSupporterクラスをab_commonプロジェクトに移動した。

File size: 15.2 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{
[206]308 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
[209]325void CollectProcedures( const BasicSource &source, UserProcs &userProcs, DllProcs &dllProcs )
[206]326{
[4]327 extern HANDLE hHeap;
328 int i,i2,i3;
329 char temporary[8192];
330
[99]331 // 名前空間管理
[199]332 NamespaceScopes &namespaceScopes = compiler.GetNamespaceSupporter().GetLivingNamespaceScopes();
[101]333 namespaceScopes.clear();
[99]334
[108]335 // Importsされた名前空間の管理
[199]336 NamespaceScopesCollection &importedNamespaces = compiler.GetNamespaceSupporter().GetImportedNamespaces();
[108]337 importedNamespaces.clear();
338
[4]339 i=-1;
340 while(1){
341 i++;
342
[206]343 if(source[i]==1&&(source[i+1]==ESC_CLASS||source[i+1]==ESC_INTERFACE)){
[4]344 /* Class ~ End Class
345 Interface ~ End Interface
346 を飛び越す */
[206]347 i3=GetEndXXXCommand(source[i+1]);
[4]348 for(i+=2,i2=0;;i++,i2++){
[206]349 if(source[i]=='\0') break;
350 if(source[i]==1&&source[i+1]==(char)i3){
[4]351 i++;
352 break;
353 }
354 }
[206]355 if(source[i]=='\0') break;
[4]356 continue;
357 }
358
[206]359 if( source[i] == 1 && source[i+1] == ESC_NAMESPACE ){
[99]360 for(i+=2,i2=0;;i2++,i++){
[206]361 if( IsCommandDelimitation( source[i] ) ){
[99]362 temporary[i2]=0;
363 break;
364 }
[206]365 temporary[i2]=source[i];
[99]366 }
367 namespaceScopes.push_back( temporary );
368
369 continue;
370 }
[206]371 else if( source[i] == 1 && source[i+1] == ESC_ENDNAMESPACE ){
[99]372 if( namespaceScopes.size() <= 0 ){
[465]373 compiler.errorMessenger.Output(12, "End Namespace", i );
[99]374 }
375 else{
376 namespaceScopes.pop_back();
377 }
378
379 i += 2;
380 continue;
381 }
[206]382 else if( source[i] == 1 && source[i+1] == ESC_IMPORTS ){
[108]383 for(i+=2,i2=0;;i2++,i++){
[206]384 if( IsCommandDelimitation( source[i] ) ){
[108]385 temporary[i2]=0;
386 break;
387 }
[206]388 temporary[i2]=source[i];
[108]389 }
[199]390 if( !compiler.GetNamespaceSupporter().ImportsNamespace( temporary ) )
[169]391 {
[465]392 compiler.errorMessenger.Output(64,temporary,cp );
[169]393 }
[99]394
[108]395 continue;
396 }
[206]397 else if( source[i] == 1 && source[i+1] == ESC_CLEARNAMESPACEIMPORTED ){
[108]398 importedNamespaces.clear();
399 continue;
400 }
401
[206]402 if(source[i]==1&&source[i+1]==ESC_DECLARE){
[4]403 for(i+=2,i2=0;;i2++,i++){
[206]404 if(source[i]=='\n'){
[4]405 temporary[i2]=0;
406 break;
407 }
[206]408 temporary[i2]=source[i];
409 if(source[i]=='\0') break;
[4]410 }
[209]411 dllProcs.Add(namespaceScopes,temporary,i);
[4]412
413 continue;
414 }
[206]415 if(source[i]==1&&(source[i+1]==ESC_SUB||source[i+1]==ESC_FUNCTION||source[i+1]==ESC_MACRO)){
416 char statementChar = source[i+1];
[108]417
[4]418 for(i2=0;;i2++,i++){
[206]419 if(IsCommandDelimitation(source[i])){
[4]420 temporary[i2]=0;
421 break;
422 }
[206]423 temporary[i2]=source[i];
424 if(source[i]=='\0') break;
[4]425 }
[353]426 userProcs.AddUserProc(namespaceScopes, importedNamespaces, temporary,i,false,NULL,false);
[4]427
[108]428 /* Sub ~ End Sub
429 Function ~ End Function
430 Macro ~ End Macro
431 を飛び越す */
432 char endStatementChar = GetEndXXXCommand( statementChar );
433 for(i2=0;;i++,i2++){
[206]434 if( source[i] == '\0' ) break;
435 if( source[i] == 1 && source[i+1] == endStatementChar ){
[108]436 i++;
437 break;
438 }
439 }
[206]440 if(source[i]=='\0') break;
[4]441 continue;
442 }
443
444 //次の行
445 for(;;i++){
[206]446 if(IsCommandDelimitation(source[i])) break;
[4]447 }
[206]448 if(source[i]=='\0') break;
[4]449 }
[308]450
451 ////////////
452 // 特殊関数
453 ////////////
454 namespaceScopes.clear();
455 importedNamespaces.clear();
456
457 compiler.globalAreaProcName = "_System_GlobalArea_" + compiler.GetModuleName();
458 sprintf(temporary,"%c%c%s()",1,ESC_SUB,compiler.globalAreaProcName.c_str());
[353]459 userProcs.AddUserProc( namespaceScopes, importedNamespaces, temporary,0,false,NULL,false);
[4]460}
461
[94]462bool IsNeedProcCompile(){
[265]463 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
464 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
[206]465 {
[265]466 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
[206]467 if( pUserProc->IsUsing() && pUserProc->IsCompiled() == false ){
468 return true;
[94]469 }
470 }
471 return false;
472}
[316]473
474void CompileBufferInProcedure( const UserProc &userProc ){
475 if( userProc.IsCompiled() ) return;
476
477 _compile_proc( &userProc );
478
479/*
480 // ログを履く
481 char temporary[8192];
482 temporary[0]=0;
483 lstrcat( temporary, "------------------------------------------------------------------\n" );
484 sprintf( temporary + lstrlen(temporary), "【 %s のコード情報】\n", userProc.GetFullName().c_str() );
485 sprintf( temporary + lstrlen(temporary), "code size: %d bytes\n", userProc.GetCodeSize() );
486 lstrcat( temporary, "------------------------------------------------------------------\n" );
487 lstrcat( temporary, "\n" );
488 Smoothie::Logger::Put( temporary );*/
489}
490void CompileLocal(){
491 if( compiler.IsDll() )
492 {
493 //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
494 const UserProc *pUserProc=GetSubHash("_System_InitDllGlobalVariables");
495 if(pUserProc){
496 CompileBufferInProcedure( *pUserProc );
497 }
[465]498 else compiler.errorMessenger.Output(300,NULL,cp);
[316]499 }
500 else
501 {
502 // グローバル領域を一番初めにコンパイルする
503 extern const UserProc *pSub_System_GlobalArea;
504 CompileBufferInProcedure( *pSub_System_GlobalArea );
505 }
506
507 //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
508 extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
509 pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
510
[355]511 //_System_TypeBase_InitializeUserTypesForBaseTypeは一番最後にコンパイル
512 extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType;
513 pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType->CompleteCompile();
514
[316]515 //_System_InitStaticLocalVariablesは一番最後にコンパイル
516 //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
517 extern const UserProc *pSub_System_InitStaticLocalVariables;
518 pSub_System_InitStaticLocalVariables->CompleteCompile();
519
520 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
521 extern const UserProc *pSub_System_Call_Destructor_of_GlobalObject;
522 pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
523
[319]524 // _System_CGarbageCollection.RegisterGlobalRootsは一番最後にコンパイル
525 extern const UserProc *pUserProc_System_CGarbageCollection_RegisterGlobalRoots;
526 pUserProc_System_CGarbageCollection_RegisterGlobalRoots->CompleteCompile();
527
[316]528repeat:
529 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
530 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
531 {
532 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
533 CompileBufferInProcedure( *pUserProc );
534 }
535
536 if( IsNeedProcCompile() ){
537 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
538 goto repeat;
539 }
540
541 if( !compiler.IsStaticLibrary() )
542 {
543 //_System_TypeBase_InitializeUserTypesは最後のほうでコンパイル
544 pSubStaticMethod_System_TypeBase_InitializeUserTypes->KillCompileStatus();
545 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypes );
546
[355]547 //_System_TypeBase_InitializeUserTypesForBaseTypeは最後のほうでコンパイル
548 pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType->KillCompileStatus();
549 CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypesForBaseType );
550
[316]551 if( IsNeedProcCompile() ){
552 //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
553
554 compiler.GetObjectModule().meta.GetUserProcs().Iterator_Reset();
555 while( compiler.GetObjectModule().meta.GetUserProcs().Iterator_HasNext() )
556 {
557 UserProc *pUserProc = compiler.GetObjectModule().meta.GetUserProcs().Iterator_GetNext();
558 CompileBufferInProcedure( *pUserProc );
559 }
560 }
561
562 //_System_InitStaticLocalVariablesは一番最後にコンパイル
563 pSub_System_InitStaticLocalVariables->KillCompileStatus();
564 CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
565
566 //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
567 pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
568 CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
[319]569
570 // _System_CGarbageCollection.RegisterGlobalRootsは一番最後にコンパイル
571 pUserProc_System_CGarbageCollection_RegisterGlobalRoots->KillCompileStatus();
572 CompileBufferInProcedure( *pUserProc_System_CGarbageCollection_RegisterGlobalRoots );
[316]573 }
574}
Note: See TracBrowser for help on using the repository browser.