source: dev/trunk/abdev/BasicCompiler_Common/DebugMiddleFile.cpp@ 206

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

コード全体のリファクタリングを実施

File size: 26.1 KB
RevLine 
[206]1#include "stdafx.h"
2
[182]3#include <jenga/include/smoothie/Smoothie.h>
4
[193]5#include <Compiler.h>
[206]6#include <Class.h>
7#include <Variable.h>
[182]8
[4]9#include "../BasicCompiler_Common/common.h"
[206]10#include "../BasicCompiler_Common/DebugSection.h"
[4]11
12#ifdef _AMD64_
13#include "../BasicCompiler64/opcode.h"
14#else
[5]15#include "../BasicCompiler32/opcode.h"
[4]16#endif
17
18#define MDLFILE_VER 0x70000003
19
20
[75]21void SetLpIndex_DebugFile(char *buffer,int *p,const Type &type){
22 if(NATURAL_TYPE(type.GetBasicType())==DEF_OBJECT || NATURAL_TYPE(type.GetBasicType())==DEF_STRUCT){
[131]23 lstrcpy(buffer+(*p),type.GetClass().GetName().c_str());
[4]24 (*p)+=lstrlen(buffer+(*p))+1;
25 }
26 else{
[75]27 *(LONG_PTR *)(buffer+(*p))=type.GetIndex();
[4]28 (*p)+=sizeof(LONG_PTR);
29 }
30}
31
32
[75]33void GetLpIndex_DebugFile(char *buffer,int *p,Type &type){
34 if(NATURAL_TYPE(type.GetBasicType())==DEF_OBJECT || NATURAL_TYPE(type.GetBasicType())==DEF_STRUCT){
[4]35 char szClassName[VN_SIZE];
36 lstrcpy(szClassName,buffer+(*p));
37 (*p)+=lstrlen(buffer+(*p))+1;
38
[201]39 type.SetClassPtr( compiler.GetMeta().GetClasses().Find(szClassName) );
[4]40 }
41 else{
[75]42 type.SetIndex( *(LONG_PTR *)(buffer+(*p)) );
[4]43 (*p)+=sizeof(LONG_PTR);
44 }
45}
46
47
48
49CDebugSection::~CDebugSection(){
50 if(pobj_DBClass) DeleteDebugInfo();
51 if(buffer){
52 HeapDefaultFree(buffer);
53 buffer=0;
54 }
55}
56void CDebugSection::make(void){
57 extern INCLUDEFILEINFO IncludeFileInfo;
[206]58 int i2,i3,BufferSize;
[4]59
60 if(buffer){
61 HeapDefaultFree(buffer);
62 buffer=0;
63 }
64
65 i2=0;
66
67 extern char *basbuf;
68 BufferSize=lstrlen(basbuf)+65535;
69 buffer=(char *)HeapAlloc(hHeap,0,BufferSize);
70
71 //デバッグ用ファイルのバージョン
72 *(long *)(buffer+i2)=MDLFILE_VER;
73 i2+=sizeof(long);
74
75 //プラットフォームのビット数
76 *(long *)(buffer+i2)=PLATFORM;
77 i2+=sizeof(long);
78
79 //インクルード情報
80 *(long *)(buffer+i2)=IncludeFileInfo.FilesNum;
81 i2+=sizeof(long);
82 for(i3=0;i3<IncludeFileInfo.FilesNum;i3++){
83 lstrcpy(buffer+i2,IncludeFileInfo.ppFileNames[i3]);
84 i2+=lstrlen(buffer+i2)+1;
85 }
86 buffer[i2++]=0;
87 for(i3=0;;i3++){
88 buffer[i2++]=(char)IncludeFileInfo.LineOfFile[i3];
89 if(IncludeFileInfo.LineOfFile[i3]==-1) break;
90 }
91
92 //ソースコード
93 lstrcpy(buffer+i2,basbuf);
94 i2+=lstrlen(buffer+i2)+1;
95
96
97 ////////////////////////
98 // コードと行番号の関係
99 ////////////////////////
100 extern int MaxLineInfoNum;
101 extern LINEINFO *pLineInfo;
102
103 //バッファが足りない場合は再確保
104 if(MaxLineInfoNum*sizeof(LINEINFO)<32768) i3=32768;
105 else i3=MaxLineInfoNum*sizeof(LINEINFO)+32768;
106 if(BufferSize<i2+i3){
107 BufferSize+=i3;
108 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
109 }
110
111 *(long *)(buffer+i2)=MaxLineInfoNum;
112 i2+=sizeof(long);
113 memcpy(buffer+i2,pLineInfo,MaxLineInfoNum*sizeof(LINEINFO));
114 i2+=MaxLineInfoNum*sizeof(LINEINFO);
115
116
117
118 ////////////////////////////////////////////
119 // クラス情報(名前のみ。詳細は後で保存)
120 ////////////////////////////////////////////
121
122 //イテレータをリセット
[193]123 compiler.GetMeta().GetClasses().Iterator_Reset();
[4]124
125 //個数
[193]126 *(long *)(buffer+i2)=compiler.GetMeta().GetClasses().Iterator_GetMaxCount();
[4]127 i2+=sizeof(long);
128
[193]129 while(compiler.GetMeta().GetClasses().Iterator_HasNext()){
[4]130 CClass *pobj_c;
[193]131 pobj_c=compiler.GetMeta().GetClasses().Iterator_GetNext();
[4]132
133 //クラス名
[131]134 lstrcpy(buffer+i2,pobj_c->GetName().c_str());
[4]135 i2+=lstrlen(buffer+i2)+1;
136 }
137
138
139
140 //////////////////
141 // TypeDef情報
142 //////////////////
[193]143 *(long *)(buffer+i2)=(int)compiler.GetMeta().GetTypeDefs().size();
[4]144 i2+=sizeof(long);
[193]145 for(i3=0;i3<(int)compiler.GetMeta().GetTypeDefs().size();i3++){
146 lstrcpy(buffer+i2,compiler.GetMeta().GetTypeDefs()[i3].GetName().c_str() );
[4]147 i2+=lstrlen(buffer+i2)+1;
148
[193]149 lstrcpy(buffer+i2,compiler.GetMeta().GetTypeDefs()[i3].GetBaseName().c_str() );
[4]150 i2+=lstrlen(buffer+i2)+1;
151
152 //バッファが足りない場合は再確保
153 if(BufferSize<i2+32768){
154 BufferSize+=32768;
155 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
156 }
157 }
158
159
160 //グローバル変数情報
[206]161 *(long *)(buffer+i2)=(int)compiler.GetMeta().GetGlobalVars().size();
[4]162 i2+=sizeof(long);
[206]163 BOOST_FOREACH( Variable *pVar, compiler.GetMeta().GetGlobalVars() ){
[4]164 //変数名
[75]165 lstrcpy(buffer+i2,pVar->GetName().c_str());
[4]166 i2+=lstrlen(buffer+i2)+1;
167
168 //型
[206]169 *(long *)(buffer+i2)=pVar->GetType().GetBasicType();
[4]170 i2+=sizeof(long);
171
172 //型の拡張情報
[206]173 SetLpIndex_DebugFile(buffer,&i2,pVar->GetType());
[4]174
[75]175 buffer[i2++] = pVar->IsRef() ? 1 : 0;
[4]176
[75]177 buffer[i2++] = pVar->IsArray() ? 1 : 0;
[4]178
[75]179 if(pVar->IsArray()){
[206]180 *(long *)(buffer+i2)=(int)pVar->GetSubscripts().size();
181 i2+=sizeof(long);
182 BOOST_FOREACH( int indexMax, pVar->GetSubscripts() )
183 {
184 *(long *)(buffer+i2)=indexMax;
[4]185 i2+=sizeof(long);
186 }
187 }
188
189 //レキシカルスコープ情報
[206]190 *(long *)(buffer+i2)=pVar->GetScopeStartAddress();
[4]191 i2+=sizeof(long);
[206]192 *(long *)(buffer+i2)=pVar->GetScopeEndAddress();
[4]193 i2+=sizeof(long);
[206]194 *(long *)(buffer+i2)=pVar->GetScopeLevel();
[4]195 i2+=sizeof(long);
196
197 //メモリ位置
[206]198 *(long *)(buffer+i2)=pVar->GetOffsetAddress();
[4]199 i2+=sizeof(long);
200
201 //バッファが足りない場合は再確保
202 if(BufferSize<i2+32768){
203 BufferSize+=32768;
204 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
205 }
206 }
207
208 //グローバル実行領域のサイズ
209 extern int GlobalOpBufferSize;
210 *(long *)(buffer+i2)=GlobalOpBufferSize;
211 i2+=sizeof(long);
212
213 //プロシージャ情報
[206]214 *(long *)(buffer+i2) = compiler.GetMeta().GetUserProcs().Iterator_GetMaxCount();
[4]215 i2+=sizeof(long);
[206]216 compiler.GetMeta().GetUserProcs().Iterator_Reset();
217 while( compiler.GetMeta().GetUserProcs().Iterator_HasNext() )
218 {
219 UserProc *pUserProc = compiler.GetMeta().GetUserProcs().Iterator_GetNext();
[4]220
[206]221 if(pUserProc->GetParentClassPtr()){
222 lstrcpy(buffer+i2,pUserProc->GetParentClassPtr()->GetName().c_str());
[4]223 i2+=lstrlen(buffer+i2)+1;
[206]224 }
225 else{
226 lstrcpy(buffer+i2,"");
227 i2+=lstrlen(buffer+i2)+1;
228 }
[4]229
[206]230 //ID
231 *(long *)(buffer+i2)=pUserProc->GetId();
232 i2+=sizeof(long);
[4]233
[206]234 //関数名
235 lstrcpy(buffer+i2,pUserProc->GetName().c_str());
236 i2+=lstrlen(buffer+i2)+1;
[4]237
[206]238 *(long *)(buffer+i2)=pUserProc->GetBeginOpAddress();
239 i2+=sizeof(long);
240 *(long *)(buffer+i2)=pUserProc->GetEndOpAddress();
241 i2+=sizeof(long);
[4]242
[206]243 //ローカル変数情報
244 *(long *)(buffer+i2)=(int)pUserProc->GetLocalVars().size();
245 i2+=sizeof(long);
[4]246
[206]247 //バッファが足りない場合は再確保
248 if(BufferSize<i2+32768){
249 BufferSize+=32768;
250 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
251 }
[4]252
[206]253 BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
254 lstrcpy(buffer+i2,pVar->GetName().c_str());
255 i2+=lstrlen(buffer+i2)+1;
[4]256
[206]257 //型
258 *(long *)(buffer+i2)=pVar->GetType().GetBasicType();
259 i2+=sizeof(long);
[4]260
[206]261 //型の拡張情報
262 SetLpIndex_DebugFile(buffer,&i2,pVar->GetType());
[4]263
[206]264 //参照型パラメータかどうか
265 buffer[i2++] = pVar->IsRef() ? 1 : 0;
[4]266
[206]267 //配列かどうか
268 buffer[i2++] = pVar->IsArray() ? 1 : 0;
[4]269
[206]270 //配列要素
271 if(pVar->IsArray()){
272 *(long *)(buffer+i2)=(int)pVar->GetSubscripts().size();
[4]273 i2+=sizeof(long);
[206]274 BOOST_FOREACH( int indexMax, pVar->GetSubscripts() )
275 {
276 *(long *)(buffer+i2)=indexMax;
277 i2+=sizeof(long);
278 }
279 }
[4]280
[206]281 //レキシカルスコープ情報
282 *(long *)(buffer+i2)=pVar->GetScopeStartAddress();
283 i2+=sizeof(long);
284 *(long *)(buffer+i2)=pVar->GetScopeEndAddress();
285 i2+=sizeof(long);
286 *(long *)(buffer+i2)=pVar->GetScopeLevel();
287 i2+=sizeof(long);
[4]288
[206]289 //メモリ位置
290 *(long *)(buffer+i2)=pVar->GetOffsetAddress();
291 i2+=sizeof(long);
[4]292
293
[206]294
295
296 //バッファが足りない場合は再確保
297 if(BufferSize<i2+32768){
298 BufferSize+=32768;
299 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
[4]300 }
301 }
302 }
303
304
305
306 ///////////////////
307 // クラス情報
308 ///////////////////
309
310 //イテレータをリセット
[193]311 compiler.GetMeta().GetClasses().Iterator_Reset();
[4]312
[193]313 while(compiler.GetMeta().GetClasses().Iterator_HasNext()){
[4]314 CClass *pobj_c;
[193]315 pobj_c=compiler.GetMeta().GetClasses().Iterator_GetNext();
[4]316
317
318 //クラス名
[131]319 lstrcpy(buffer+i2,pobj_c->GetName().c_str());
[4]320 i2+=lstrlen(buffer+i2)+1;
321
322 //仮想関数の数
[141]323 *(long *)(buffer+i2)=pobj_c->GetVtblNum();
[4]324 i2+=sizeof(long);
325
326 //アラインメント
327 *(long *)(buffer+i2)=pobj_c->iAlign;
328 i2+=sizeof(long);
329
[206]330 // 動的メンバ
[182]331 *(long *)(buffer+i2)=(int)pobj_c->GetDynamicMembers().size();
[4]332 i2+=sizeof(long);
[182]333 BOOST_FOREACH( CMember *member, pobj_c->GetDynamicMembers() ){
[137]334 // 名前
[140]335 lstrcpy(buffer+i2,member->GetName().c_str());
[4]336 i2+=lstrlen(buffer+i2)+1;
337
[137]338 // 型
[140]339 *(long *)(buffer+i2)=member->GetType().GetBasicType();
[4]340 i2+=sizeof(long);
341
[137]342 // 型の拡張情報
[140]343 SetLpIndex_DebugFile(buffer,&i2,member->GetType());
[4]344
[137]345 // アクセシビリティ
[140]346 *(Prototype::Accessibility *)(buffer+i2)=member->GetAccessibility();
[137]347 i2+=sizeof(Prototype::Accessibility);
[4]348
[206]349 *(long *)(buffer+i2)=(int)member->GetSubscripts().size();
350 i2+=sizeof(long);
351 BOOST_FOREACH( int indexMax, member->GetSubscripts() )
352 {
353 *(long *)(buffer+i2)=indexMax;
354 i2+=sizeof(long);
355 }
[137]356
[4]357 //バッファが足りない場合は再確保
358 if(BufferSize<i2+32768){
359 BufferSize+=32768;
360 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
361 }
362 }
363
[206]364 // 動的メソッド
[182]365 *(long *)(buffer+i2)=(long)pobj_c->GetMethods().size();
[4]366 i2+=sizeof(long);
[182]367 BOOST_FOREACH( const CMethod *pMethod, pobj_c->GetMethods() ){
[137]368 *(Prototype::Accessibility *)(buffer+i2)=pMethod->GetAccessibility();
369 i2+=sizeof(Prototype::Accessibility);
[135]370 if( pMethod->GetInheritsClassPtr() ){
371 lstrcpy(buffer+i2,pMethod->GetInheritsClassPtr()->GetName().c_str());
[4]372 i2+=lstrlen(buffer+i2)+1;
373 }
374 else{
375 lstrcpy(buffer+i2,"");
376 i2+=lstrlen(buffer+i2)+1;
377 }
[206]378 lstrcpy(buffer+i2,pMethod->GetUserProc().GetName().c_str());
[4]379 i2+=lstrlen(buffer+i2)+1;
380 }
381
382 //静的メンバ
[182]383 *(long *)(buffer+i2)=(long)pobj_c->GetStaticMembers().size();
[4]384 i2+=sizeof(long);
[182]385 BOOST_FOREACH( CMember *member, pobj_c->GetStaticMembers() ){
[137]386 // 名前
[135]387 lstrcpy(buffer+i2,member->GetName().c_str());
[4]388 i2+=lstrlen(buffer+i2)+1;
389
[137]390 // 型
391 *(long *)(buffer+i2)=member->GetType().GetBasicType();
[4]392 i2+=sizeof(long);
393
[137]394 // 型の拡張情報
395 SetLpIndex_DebugFile(buffer,&i2,member->GetType());
[4]396
[137]397 // アクセシビリティ
398 *(Prototype::Accessibility *)(buffer+i2)=member->GetAccessibility();
399 i2+=sizeof(Prototype::Accessibility);
[4]400
[206]401 // 配列
402 *(long *)(buffer+i2)=(int)member->GetSubscripts().size();
403 i2+=sizeof(long);
404 BOOST_FOREACH( int indexMax, member->GetSubscripts() )
405 {
406 *(long *)(buffer+i2)=indexMax;
407 i2+=sizeof(long);
408 }
[137]409
[4]410 //バッファが足りない場合は再確保
411 if(BufferSize<i2+32768){
412 BufferSize+=32768;
413 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
414 }
415 }
416
417 //バッファが足りない場合は再確保
418 if(BufferSize<i2+32768){
419 BufferSize+=32768;
420 buffer=(char *)HeapReAlloc(hHeap,0,buffer,BufferSize);
421 }
422 }
423
424 length=i2;
425}
426
427char *CDebugSection::MakeSingleStepCode(void){
428 char *buffer;
429 buffer=(char *)HeapAlloc(hHeap,0,SizeOf_CodeSection);
430
431 memcpy(buffer,OpBuffer,SizeOf_CodeSection);
432
433 int i2;
434 for(i2=0;i2<MaxLineInfoNum;i2++){
435 if(!(
436 pLineInfo[i2].dwCodeType&CODETYPE_SYSTEMPROC||
437 pLineInfo[i2].dwCodeType&CODETYPE_DEBUGPROC
438 )){
439 //int 3
440 buffer[pLineInfo[i2].TopObp]=(char)0xCC;
441 }
442 }
443
444 return buffer;
445}
446BOOL CDebugSection::__load(void){
[206]447 int i2,i3,i4,num;
[4]448 char temp2[MAX_PATH],*temp5;
449
[206]450 compiler.pCompilingClass = NULL;
[16]451
[4]452 i2=0;
453
454 //デバッグ用ファイルのバージョンをチェック
455 if(*(long *)(buffer+i2)<MDLFILE_VER){
456 HeapDefaultFree(buffer);
457 return 0;
458 }
459 i2+=sizeof(long);
460
461 //プラットフォームのビット数をチェック
462 if(*(long *)(buffer+i2)!=PLATFORM){
463 HeapDefaultFree(buffer);
464 return 0;
465 }
466 i2+=sizeof(long);
467
468 //インクルード情報
469 IncludeFileInfo.FilesNum=*(long *)(buffer+i2);
470 i2+=sizeof(long);
[191]471 IncludeFileInfo.ppFileNames=(char **)malloc(IncludeFileInfo.FilesNum*sizeof(char *));
[4]472 for(i3=0;i3<IncludeFileInfo.FilesNum;i3++){
473 if(buffer[i2]=='\0') break;
[191]474 IncludeFileInfo.ppFileNames[i3]=(char *)malloc(lstrlen(buffer+i2)+1);
[4]475 lstrcpy(IncludeFileInfo.ppFileNames[i3],buffer+i2);
476 i2+=lstrlen(buffer+i2)+1;
477 }
478 for(i2++,i3=0;;i2++,i3++){
479 IncludeFileInfo.LineOfFile[i3]=(long)buffer[i2];
480 if(IncludeFileInfo.LineOfFile[i3]==-1) break;
481 }
482
483 //ソースコード
484 i2++;
[88]485 source.SetBuffer( buffer + i2 );
[15]486 i2+=lstrlen(buffer+i2)+1;
[4]487
488 //コードと行番号の関係
489 MaxLineInfoNum=*(long *)(buffer+i2);
490 i2+=sizeof(long);
491 pLineInfo=(LINEINFO *)HeapAlloc(hHeap,0,MaxLineInfoNum*sizeof(LINEINFO)+1);
492 memcpy(pLineInfo,buffer+i2,MaxLineInfoNum*sizeof(LINEINFO));
493 i2+=MaxLineInfoNum*sizeof(LINEINFO);
494
495
496 ///////////////////////////////////////////
497 // クラス情報(名前のみ。詳細は後で取得)
498 ///////////////////////////////////////////
499
[206]500 this->pobj_DBClass=new Classes();
[4]501
502 int iMaxClassCount;
503 iMaxClassCount=*(long *)(buffer+i2);
504 i2+=sizeof(long);
505 for(i3=0;i3<iMaxClassCount;i3++){
506 //クラス名
[101]507 // TODO: 名前空間が未完成
[182]508 this->pobj_DBClass->Add(NamespaceScopes(),NamespaceScopesCollection(),buffer+i2,0);
[4]509 i2+=lstrlen(buffer+i2)+1;
510 }
511
[193]512 compiler.GetMeta().SetClasses( this->pobj_DBClass );
[4]513
514
515 //////////////////
516 // TypeDef情報
517 //////////////////
518
519 //初期化
[193]520 compiler.GetMeta().GetTypeDefs().clear();
[4]521
522 //個数を取得
523 num=*(long *)(buffer+i2);
524 i2+=sizeof(long);
525 for(i3=0;i3<num;i3++){
526 temp5=buffer+i2;
527 i2+=lstrlen(buffer+i2)+1;
528
[113]529 // 名前空間に未対応
[193]530 compiler.GetMeta().GetTypeDefs().push_back( TypeDef( NamespaceScopes(), temp5, buffer+i2, -1 ) );
[4]531
532 i2+=lstrlen(buffer+i2)+1;
533 }
534
535 //定数を取得
536 GetConstInfo();
[206]537 this->globalConsts = compiler.GetMeta().GetGlobalConsts();
538 this->globalConstMacros = compiler.GetMeta().GetGlobalConstMacros();
[4]539
540 //グローバル変数情報
[206]541 compiler.GetMeta().GetGlobalVars().clear();
[75]542 int maxGlobalVars=*(long *)(buffer+i2);
[4]543 i2+=sizeof(long);
[75]544 for(i3=0;i3<maxGlobalVars;i3++){
[4]545
546 //変数名
[75]547 char *name = buffer+i2;
[4]548 i2+=lstrlen(buffer+i2)+1;
549
[75]550 int basicType = *(long *)(buffer+i2);
[4]551 i2+=sizeof(long);
552
[75]553 Type type( basicType );
554 GetLpIndex_DebugFile(buffer,&i2,type);
[4]555
[75]556 bool isRef = (buffer[i2++]) ? true:false;
[4]557
[75]558 bool isArray = (buffer[i2++]) ? true:false;
559
[206]560 Variable *pVar = new Variable( name, type, false, isRef, "" );
[75]561
562 if(isArray){
[206]563 Subscripts subscripts;
564 int nSubScriptsNum = *(long *)(buffer+i2);
565 i2+=sizeof(long);
566 for( int i=0; i<nSubScriptsNum; i++ )
567 {
568 subscripts.push_back( *(long *)(buffer+i2) );
[4]569 i2+=sizeof(long);
570 }
[75]571
[206]572 pVar->SetArray( subscripts );
[4]573 }
574
575 //レキシカルスコープ情報
[206]576 pVar->SetScopeStartAddress( *(long *)(buffer+i2) );
[4]577 i2+=sizeof(long);
[206]578 pVar->SetScopeEndAddress( *(long *)(buffer+i2) );
[4]579 i2+=sizeof(long);
[206]580 pVar->SetScopeLevel( *(long *)(buffer+i2) );
[4]581 i2+=sizeof(long);
582
583 //メモリ位置
[206]584 pVar->SetOffsetAddress( *(long *)(buffer+i2) );
[4]585 i2+=sizeof(long);
[75]586
587 //変数を追加
[206]588 compiler.GetMeta().GetGlobalVars().push_back( pVar );
[4]589 }
590
591 //グローバル実行領域のサイズ
592 GlobalOpBufferSize=*(long *)(buffer+i2);
593 i2+=sizeof(long);
594
595 //プロシージャ情報
[206]596 userProcs.Clear();
597 int subNum = *(long *)(buffer+i2);
[4]598 i2+=sizeof(long);
[206]599 for(int i6=0;i6<subNum;i6++){
[4]600 char szParentClassName[VN_SIZE];
601 lstrcpy(szParentClassName,buffer+i2);
602 i2+=lstrlen(buffer+i2)+1;
603
[114]604 const CClass *pClass = NULL;
605 if(szParentClassName[0]){
[193]606 pClass=compiler.GetMeta().GetClasses().Find(szParentClassName);
[114]607 }
[4]608
609 //ID
[75]610 int id=*(long *)(buffer+i2);
[4]611 i2+=sizeof(long);
612
613 //名前
[75]614 char *name = buffer+i2;
[4]615 i2+=lstrlen(buffer+i2)+1;
616
[75]617 // オブジェクトを生成
[100]618 // TODO: 名前空間が未完成
[206]619 UserProc *pUserProc = new UserProc( NamespaceScopes(), NamespaceScopesCollection(), name, Procedure::Function, false, false, false, id );
[75]620 pUserProc->SetParentClass( pClass );
621
[206]622 pUserProc->SetBeginOpAddress( *(long *)(buffer+i2) );
[4]623 i2+=sizeof(long);
[206]624 pUserProc->SetEndOpAddress( *(long *)(buffer+i2) );
[4]625 i2+=sizeof(long);
626
[75]627 pUserProc->CompleteCompile();
[4]628
629 //ローカル変数情報
[206]630 pUserProc->GetLocalVars().clear();
[75]631 int maxLocalVar=*(long *)(buffer+i2);
[4]632 i2+=sizeof(long);
[75]633 for(i3=0;i3<maxLocalVar;i3++){
634 //変数名
635 char *name = buffer+i2;
[4]636 i2+=lstrlen(buffer+i2)+1;
637
[75]638 int basicType = *(long *)(buffer+i2);
[4]639 i2+=sizeof(long);
640
[75]641 Type type( basicType );
642 GetLpIndex_DebugFile(buffer,&i2,type);
[4]643
[75]644 bool isRef = (buffer[i2++]) ? true:false;
[4]645
[75]646 bool isArray = (buffer[i2++]) ? true:false;
647
[206]648 Variable *pVar = new Variable( name, type, false, isRef, "" );
[75]649
650 if(isArray){
[206]651 Subscripts subscripts;
652 int nSubScriptsNum = *(long *)(buffer+i2);
653 i2+=sizeof(long);
654 for( int i=0; i<nSubScriptsNum; i++ )
655 {
656 subscripts.push_back( *(long *)(buffer+i2) );
[4]657 i2+=sizeof(long);
658 }
[75]659
[206]660 pVar->SetArray( subscripts );
[4]661 }
662
663 //レキシカルスコープ情報
[206]664 pVar->SetScopeStartAddress( *(long *)(buffer+i2) );
[4]665 i2+=sizeof(long);
[206]666 pVar->SetScopeEndAddress( *(long *)(buffer+i2) );
[4]667 i2+=sizeof(long);
[206]668 pVar->SetScopeLevel( *(long *)(buffer+i2) );
[4]669 i2+=sizeof(long);
670
[75]671 //メモリ位置
[206]672 pVar->SetOffsetAddress( *(long *)(buffer+i2) );
[4]673 i2+=sizeof(long);
[75]674
675 //変数を追加
[206]676 pUserProc->GetLocalVars().push_back( pVar );
[4]677 }
678
679
680 /////////////////////////////////
[75]681 // 格納位置を計算してpUserProcをセット
[4]682 /////////////////////////////////
683
[206]684 // ハッシュに追加
685 if( !userProcs.Insert( pUserProc, -1 ) )
686 {
687 //return NULL;
[4]688 }
689 }
[206]690 userProcs.Iterator_Init();
[4]691
692 //クラス情報
693 CClass *pobj_c;
694 for(i3=0;i3<iMaxClassCount;i3++){
695 //クラス名
696 char szClassName[VN_SIZE];
697 lstrcpy(szClassName,buffer+i2);
698 i2+=lstrlen(buffer+i2)+1;
699
[193]700 pobj_c = const_cast<CClass *>( compiler.GetMeta().GetClasses().Find(szClassName) );
[4]701
702 //仮想関数の数
[141]703 pobj_c->SetVtblNum( *(long *)(buffer+i2) );
[4]704 i2+=sizeof(long);
705
706 //アラインメント
707 pobj_c->iAlign=*(long *)(buffer+i2);
708 i2+=sizeof(long);
709
[206]710 // 動的メンバ
[140]711 int nDynamicMember = *(long *)(buffer+i2);
[4]712 i2+=sizeof(long);
[140]713 for( i4=0; i4<nDynamicMember; i4++ ){
[137]714 // 名前
715 string name = (char *)(buffer+i2);
[4]716 i2+=lstrlen(buffer+i2)+1;
717
[137]718 // 型
[75]719 Type type( *(long *)(buffer+i2) );
[4]720 i2+=sizeof(long);
721
[137]722 // 型の拡張情報
[75]723 GetLpIndex_DebugFile(buffer,&i2,type);
[4]724
[137]725 // アクセシビリティ
726 Prototype::Accessibility accessibility = *(Prototype::Accessibility *)(buffer+i2);
727 i2+=sizeof(Prototype::Accessibility);
[75]728
[206]729 Subscripts subscripts;
730 int nSubScriptsNum = *(long *)(buffer+i2);
731 i2+=sizeof(long);
732 for( int i=0; i<nSubScriptsNum; i++ )
733 {
734 subscripts.push_back( *(long *)(buffer+i2) );
735 i2+=sizeof(long);
736 }
[137]737
[206]738 CMember *member=new CMember( accessibility, name, type, false, subscripts, "", "" );
[140]739
[182]740 pobj_c->GetDynamicMembers().push_back( member );
[4]741 }
742
[206]743 // 動的メソッド
[51]744 int nMethod = *(long *)(buffer+i2);
[4]745 i2+=sizeof(long);
[51]746 for( i4=0; i4<nMethod; i4++ ){
[4]747
[137]748 Prototype::Accessibility accessibility=*(Prototype::Accessibility *)(buffer+i2);
749 i2+=sizeof(Prototype::Accessibility);
[4]750
751 char szInherits[VN_SIZE];
752 lstrcpy(szInherits,buffer+i2);
753 i2+=lstrlen(buffer+i2)+1;
754
[114]755 const CClass *pobj_InheritsClass = NULL;
[100]756 if(szInherits[0]){
[193]757 pobj_InheritsClass=compiler.GetMeta().GetClasses().Find(szInherits);
[100]758 }
[4]759
760 lstrcpy(temp2,buffer+i2);
761 i2+=lstrlen(buffer+i2)+1;
762
[114]763 const CClass *pobj_temp_c;
[100]764 pobj_temp_c=pobj_InheritsClass;
[4]765 if(pobj_temp_c==0) pobj_temp_c=pobj_c;
[206]766
767 UserProc *pUserProc = compiler.GetMeta().GetUserProcs().GetHashArrayElement( temp2 );
768 while(pUserProc){
769 if( pUserProc->GetName() == temp2 &&pUserProc->GetParentClassPtr()==pobj_temp_c)
770 {
771 break;
772 }
773
774 pUserProc=pUserProc->GetChainNext();
[4]775 }
[51]776
[137]777 CMethod *pMethod = new DynamicMethod( pUserProc, accessibility, 0,0,false, pobj_InheritsClass);
[100]778
[182]779 pobj_c->GetMethods().push_back( pMethod );
[4]780 }
781
782 //静的メンバ
[53]783 int nStaticMember = *(long *)(buffer+i2);
[4]784 i2+=sizeof(long);
[53]785 for( i4=0; i4<nStaticMember; i4++ ){
[137]786 // 名前
787 string name = (char *)(buffer+i2);
[4]788 i2+=lstrlen(buffer+i2)+1;
789
[137]790 // 型
[75]791 Type type( *(long *)(buffer+i2) );
[4]792 i2+=sizeof(long);
793
[137]794 // 型の拡張情報
[75]795 GetLpIndex_DebugFile(buffer,&i2,type);
[4]796
[137]797 // アクセシビリティ
798 Prototype::Accessibility accessibility = *(Prototype::Accessibility *)(buffer+i2);
799 i2+=sizeof(Prototype::Accessibility);
[75]800
[206]801 // 配列
802 Subscripts subscripts;
803 int nSubScriptsNum = *(long *)(buffer+i2);
804 i2+=sizeof(long);
805 for( int i=0; i<nSubScriptsNum; i++ )
806 {
807 subscripts.push_back( *(long *)(buffer+i2) );
808 i2+=sizeof(long);
809 }
[53]810
[206]811 CMember *member=new CMember( accessibility, name, type, false, subscripts, "", "" );
[137]812
[182]813 pobj_c->GetStaticMembers().push_back( member );
[4]814 }
815 }
816
817 HeapDefaultFree(buffer);
818 buffer=0;
819
820
821
822
[206]823 compiler.GetMeta().GetUserProcs() = userProcs;
[4]824 pSub_DebugSys_EndProc=GetSubHash("_DebugSys_EndProc");
825
826
827 SingleStepCodeBuffer=MakeSingleStepCode();
828
829
830 /////////////////////////////
831 // ブレークポイントを適用
832 /////////////////////////////
833
834 //インクルード情報
835 extern INCLUDEFILEINFO IncludeFileInfo;
836 IncludeFileInfo=this->IncludeFileInfo;
837
838 //コードと行番号の関係
839 extern int MaxLineInfoNum;
840 extern LINEINFO *pLineInfo;
841 MaxLineInfoNum=this->MaxLineInfoNum;
842 pLineInfo=this->pLineInfo;
843
844 BreakStepCodeBuffer=pobj_DBBreakPoint->update(OpBuffer,SizeOf_CodeSection);
845
846 //プロセスメモリにコピー
847 extern HANDLE hDebugProcess;
[76]848 SIZE_T accessBytes;
[4]849 WriteProcessMemory(hDebugProcess,(void *)(ULONG_PTR)(dwImageBase+dwRVA_CodeSection),
850 BreakStepCodeBuffer,
[76]851 SizeOf_CodeSection,&accessBytes);
[4]852
853
854 return 1;
855}
856
857BOOL CDebugSection::load(HMODULE hModule){
858 if(buffer){
859 HeapDefaultFree(buffer);
860 buffer=0;
861 }
862
863
864 extern HANDLE hDebugProcess;
[76]865 SIZE_T accessBytes;
[4]866 IMAGE_DOS_HEADER ImageDosHeader;
[76]867 ReadProcessMemory(hDebugProcess,hModule,&ImageDosHeader,sizeof(IMAGE_DOS_HEADER),&accessBytes);
[4]868
869 int pe_size;
870#ifdef _AMD64_
871 IMAGE_NT_HEADERS64 pe_hdr;
872 pe_size=sizeof(IMAGE_NT_HEADERS64);
873#else
874 IMAGE_NT_HEADERS pe_hdr;
875 pe_size=sizeof(IMAGE_NT_HEADERS);
876#endif
[76]877 ReadProcessMemory(hDebugProcess,(void *)(((ULONG_PTR)hModule)+ImageDosHeader.e_lfanew),&pe_hdr,pe_size,&accessBytes);
[4]878
879 IMAGE_SECTION_HEADER *pSectionHdr;
880 pSectionHdr=(IMAGE_SECTION_HEADER *)HeapAlloc(hHeap,0,pe_hdr.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER));
881 ReadProcessMemory(hDebugProcess,
882 (void *)(((ULONG_PTR)hModule)+ImageDosHeader.e_lfanew+pe_size),
883 pSectionHdr,
884 pe_hdr.FileHeader.NumberOfSections*sizeof(IMAGE_SECTION_HEADER),
[76]885 &accessBytes);
[4]886
887 int i;
888 for(i=0;i<pe_hdr.FileHeader.NumberOfSections;i++){
889
890 //リライタブルセクション内の情報
891 if(lstrcmp((char *)pSectionHdr[i].Name,".data")==0){
892 dwRVA_RWSection=pSectionHdr[i].VirtualAddress;
893 }
894
895 //コードセクション内の情報
896 if(lstrcmp((char *)pSectionHdr[i].Name,".text")==0){
897 dwRVA_CodeSection=pSectionHdr[i].VirtualAddress;
898 SizeOf_CodeSection=pSectionHdr[i].SizeOfRawData;
899 }
900
901 //デバッグセクション内の情報
902 if(lstrcmp((char *)pSectionHdr[i].Name,".debug")==0){
903 length=pSectionHdr[i].Misc.VirtualSize;
904 buffer=(char *)HeapAlloc(hHeap,0,length+1);
905
906 ReadProcessMemory(hDebugProcess,
907 (void *)(((ULONG_PTR)hModule)+pSectionHdr[i].VirtualAddress),
908 buffer,
909 length,
[76]910 &accessBytes);
[4]911 buffer[length]=0;
912 }
913
914 }
915 HeapDefaultFree(pSectionHdr);
916
917 if(!buffer) return 0;
918
919
920 dwImageBase=(DWORD)(ULONG_PTR)hModule;
921
922
923
[191]924 if(OpBuffer) free(OpBuffer);
[188]925 OpBuffer=(char *)malloc(SizeOf_CodeSection);
[4]926
927 ReadProcessMemory(hDebugProcess,
928 (void *)(ULONG_PTR)(dwImageBase+dwRVA_CodeSection),OpBuffer,
[76]929 SizeOf_CodeSection,&accessBytes);
[4]930
931
932 return __load();
933}
934
935void CDebugSection::choice(void){
936 //イメージベース
937 extern DWORD ImageBase;
938 ImageBase=this->dwImageBase;
939
940 //リライタブルセクションのRVA
941 extern int MemPos_RWSection;
942 MemPos_RWSection=this->dwRVA_RWSection;
943
944 //コードセクションのRVAとサイズ
945 extern int MemPos_CodeSection;
946 extern int FileSize_CodeSection;
947 MemPos_CodeSection=this->dwRVA_CodeSection;
948 FileSize_CodeSection=this->SizeOf_CodeSection;
949
950 //インクルード情報
951 extern INCLUDEFILEINFO IncludeFileInfo;
952 IncludeFileInfo=this->IncludeFileInfo;
953
954 //ソースコード
[88]955 Smoothie::Lexical::source = source;
[4]956
957 //コードと行番号の関係
958 extern int MaxLineInfoNum;
959 extern LINEINFO *pLineInfo;
960 MaxLineInfoNum=this->MaxLineInfoNum;
961 pLineInfo=this->pLineInfo;
962
963 // クラス情報
[193]964 compiler.GetMeta().SetClasses( this->pobj_DBClass );
[4]965
966 //定数を取得
[206]967 compiler.GetMeta().GetGlobalConsts() = this->globalConsts;
968 compiler.GetMeta().GetGlobalConstMacros() = this->globalConstMacros;
[4]969
970 //グローバル実行領域のサイズ
971 extern int GlobalOpBufferSize;
972 GlobalOpBufferSize=this->GlobalOpBufferSize;
973
974 //プロシージャ
[206]975 compiler.GetMeta().GetUserProcs() = userProcs;
[4]976
[206]977 extern const UserProc *pSub_DebugSys_EndProc;
[4]978 pSub_DebugSys_EndProc=this->pSub_DebugSys_EndProc;
979
980 //ネイティブコードバッファ
981 extern char *OpBuffer;
982 OpBuffer=this->OpBuffer;
983}
984
985void CDebugSection::DeleteDebugInfo(void){
986 int i2;
987
988 //インクルード情報を解放
989 for(i2=0;i2<IncludeFileInfo.FilesNum;i2++)
[191]990 {
991 free(IncludeFileInfo.ppFileNames[i2]);
992 }
993 free(IncludeFileInfo.ppFileNames);
[4]994
995 //クラスに関するメモリを解放
[182]996 delete this->pobj_DBClass;
997 this->pobj_DBClass=0;
[4]998
999 //コードと行番号の関係を解放
1000 HeapDefaultFree(pLineInfo);
1001
1002 //コードバッファを解放
[182]1003 free(OpBuffer);
[4]1004 OpBuffer=0;
1005
1006 HeapDefaultFree(SingleStepCodeBuffer);
1007 SingleStepCodeBuffer=0;
1008
1009 HeapDefaultFree(BreakStepCodeBuffer);
1010 BreakStepCodeBuffer=0;
1011}
1012
1013
1014
1015CDBDebugSection::CDBDebugSection(){
1016 ppobj_ds=(CDebugSection **)HeapAlloc(hHeap,0,1);
1017 num=0;
1018}
1019CDBDebugSection::~CDBDebugSection(){
1020 int i;
1021 for(i=0;i<num;i++){
1022 delete ppobj_ds[i];
1023 }
1024 HeapDefaultFree(ppobj_ds);
1025}
1026
1027BOOL CDBDebugSection::add(HMODULE hModule){
1028 CDebugSection *pobj_d;
1029 pobj_d=new CDebugSection();
1030 if(!pobj_d->load(hModule)){
1031 //デバッグ情報が存在しないとき
1032 delete pobj_d;
1033 return 0;
1034 }
1035
1036 ppobj_ds=(CDebugSection **)HeapReAlloc(hHeap,0,ppobj_ds,(num+1)*sizeof(CDebugSection *));
1037 ppobj_ds[num]=pobj_d;
1038 num++;
1039
1040 return 1;
1041}
1042
1043void CDBDebugSection::del(HMODULE hModule){
1044 int i;
1045 for(i=0;i<num;i++){
1046 if((HMODULE)(ULONG_PTR)ppobj_ds[i]->dwImageBase==hModule){
1047 delete ppobj_ds[i];
1048
1049 num--;
1050 for(;i<num;i++){
1051 ppobj_ds[i]=ppobj_ds[i+1];
1052 }
1053 break;
1054 }
1055 }
1056}
1057
1058void CDBDebugSection::choice(int index){
1059 pobj_now=ppobj_ds[index];
1060 pobj_now->choice();
1061}
1062
1063
1064
1065CDBDebugSection *pobj_DBDebugSection;
Note: See TracBrowser for help on using the repository browser.