Index: trunk/abdev/BasicCompiler64/Compile_Statement.cpp
===================================================================
--- trunk/abdev/BasicCompiler64/Compile_Statement.cpp	(revision 243)
+++ trunk/abdev/BasicCompiler64/Compile_Statement.cpp	(revision 254)
@@ -3,5 +3,4 @@
 #include <jenga/include/smoothie/LexicalAnalysis.h>
 
-#include <LexicalScopingImpl.h>
 #include <Compiler.h>
 
@@ -192,5 +191,5 @@
 
 	//je (endif、または else まで条件ジャンプ)
-	CodeGenerator::PertialSchedule *pIfPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
+	const PertialSchedule *pIfPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
 
 
@@ -200,15 +199,15 @@
 
 	//レキシカルスコープをレベルアップ
-	GetLexicalScopes().Start( obp, SCOPE_TYPE_IF );
+	compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_IF );
 
 	int i2=CompileBuffer(ESC_ENDIF,0);
 
 	//レキシカルスコープをレベルダウン
-	GetLexicalScopes().End();
+	compiler.codeGenerator.lexicalScopes.End();
 
 
 	if(i2==ESC_ELSE){
 		//jmp (endifまで)
-		CodeGenerator::PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
+		const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
 
 		compiler.codeGenerator.opfix_JmpPertialSchedule( pIfPertialSchedule );
@@ -221,10 +220,10 @@
 
 		//レキシカルスコープをレベルアップ
-		GetLexicalScopes().Start( obp, SCOPE_TYPE_IF );
+		compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_IF );
 
 		CompileBuffer(ESC_ENDIF,0);
 
 		//レキシカルスコープをレベルダウン
-		GetLexicalScopes().End();
+		compiler.codeGenerator.lexicalScopes.End();
 
 
@@ -259,6 +258,4 @@
 void OpcodeGoto(char *Parameter){
 	extern HANDLE hHeap;
-	extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
-	extern int GotoLabelScheduleNum;
 	int i,LineNum;
 
@@ -266,16 +263,16 @@
 		i=GetLabelAddress(Parameter+1,0);
 
-		//jmp ...
-		OpBuffer[obp++]=(char)0xE9;
-		if(i==-1){
-			pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapReAlloc(hHeap,0,pGotoLabelSchedule,(GotoLabelScheduleNum+1)*sizeof(GOTOLABELSCHEDULE));
-			pGotoLabelSchedule[GotoLabelScheduleNum].pName=(char *)HeapAlloc(hHeap,0,lstrlen(Parameter+1)+1);
-			lstrcpy(pGotoLabelSchedule[GotoLabelScheduleNum].pName,Parameter+1);
-			pGotoLabelSchedule[GotoLabelScheduleNum].pos=obp;
-			pGotoLabelSchedule[GotoLabelScheduleNum].now_cp=cp;
-			GotoLabelScheduleNum++;
-		}
-		*((long *)(OpBuffer+obp))=i-(obp+sizeof(long));
-		obp+=sizeof(long);
+		if( i == -1 )
+		{
+			//jmp ...(schedule)
+			extern int obp;
+			compiler.codeGenerator.op_jmp_goto_schedule( (const std::string)(Parameter + 1), 0, cp );
+		}
+		else
+		{
+			//jmp ...
+			extern int obp;
+			compiler.codeGenerator.op_jmp( i-obp, sizeof(long), false, true );
+		}
 	}
 	else{
@@ -283,16 +280,16 @@
 		i=GetLabelAddress(0,LineNum);
 
-		//jmp ...
-		OpBuffer[obp++]=(char)0xE9;
-		if(i==-1){
-			pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapReAlloc(hHeap,0,pGotoLabelSchedule,(GotoLabelScheduleNum+1)*sizeof(GOTOLABELSCHEDULE));
-			pGotoLabelSchedule[GotoLabelScheduleNum].pName=0;
-			pGotoLabelSchedule[GotoLabelScheduleNum].line=LineNum;
-			pGotoLabelSchedule[GotoLabelScheduleNum].pos=obp;
-			pGotoLabelSchedule[GotoLabelScheduleNum].now_cp=cp;
-			GotoLabelScheduleNum++;
-		}
-		*((long *)(OpBuffer+obp))=i-(obp+sizeof(long));
-		obp+=sizeof(long);
+		if( i == -1 )
+		{
+			//jmp ...(schedule)
+			extern int obp;
+			compiler.codeGenerator.op_jmp_goto_schedule( "", LineNum, cp );
+		}
+		else
+		{
+			//jmp ...
+			extern int obp;
+			compiler.codeGenerator.op_jmp( i-obp, sizeof(long), false, true );
+		}
 	}
 }
@@ -309,13 +306,13 @@
 
 	//je (Wend まで)
-	CodeGenerator::PertialSchedule *pWhilePertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
+	const PertialSchedule *pWhilePertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
 
 	//レキシカルスコープをレベルアップ
-	GetLexicalScopes().Start( obp, SCOPE_TYPE_WHILE );
+	compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_WHILE );
 
 	//While内をコンパイル
 	CompileBuffer(0,COM_WEND);
 
-	GetLexicalScopes().CallDestructorsOfScopeEnd();
+	compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
 
 	//jmp ...
@@ -323,5 +320,5 @@
 
 	//レキシカルスコープをレベルダウン
-	GetLexicalScopes().End();
+	compiler.codeGenerator.lexicalScopes.End();
 
 	compiler.codeGenerator.opfix_JmpPertialSchedule( pWhilePertialSchedule );
@@ -364,5 +361,5 @@
 
 	//jmp ...
-	CodeGenerator::PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
+	const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
 
 	//Continueアドレスのバックアップとセット
@@ -407,5 +404,5 @@
 
 	//jmp [カウンタ減少の場合の判定を飛び越す]
-	CodeGenerator::PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
+	const PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
 
 	//jeジャンプ先のオフセット値
@@ -430,10 +427,10 @@
 
 	//レキシカルスコープをレベルアップ
-	GetLexicalScopes().Start( obp, SCOPE_TYPE_FOR );
+	compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_FOR );
 
 	//For内をコンパイル
 	CompileBuffer(0,COM_NEXT);
 
-	GetLexicalScopes().CallDestructorsOfScopeEnd();
+	compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
 
 	if(szNextVariable[0]){
@@ -447,5 +444,5 @@
 
 	//レキシカルスコープをレベルダウン
-	GetLexicalScopes().End();
+	compiler.codeGenerator.lexicalScopes.End();
 
 	//jeジャンプ先のオフセット値
@@ -466,12 +463,12 @@
 
 	//レキシカルスコープをレベルアップ
-	GetLexicalScopes().Start( obp, SCOPE_TYPE_DO );
+	compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_DO );
 
 	//Do内をコンパイル
 	CompileBuffer(0,COM_LOOP);
 
-	GetLexicalScopes().CallDestructorsOfScopeEnd();
-
-	CodeGenerator::PertialSchedule *pDoPertialSchedule = NULL;
+	compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
+
+	const PertialSchedule *pDoPertialSchedule = NULL;
 
 	extern char *basbuf;
@@ -522,8 +519,8 @@
 
 	//jmp ...
-	CodeGenerator::PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
+	const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
 
 	//レキシカルスコープをレベルダウン
-	GetLexicalScopes().End();
+	compiler.codeGenerator.lexicalScopes.End();
 
 	//jmpジャンプ先のオフセット値
@@ -539,8 +536,4 @@
 
 void OpcodeExitSub(void){
-	extern DWORD *pExitSubSchedule;
-	extern int ExitSubScheduleNum;
-	extern HANDLE hHeap;
-
 	if( UserProc::IsGlobalAreaCompiling() ){
 		SetError(12,"Exit Sub/Function",cp);
@@ -549,45 +542,32 @@
 
 	//未解放のローカルオブジェクトのデストラクタを呼び出す
-	GetLexicalScopes().CallDestructorsOfReturn();
+	compiler.codeGenerator.lexicalScopes.CallDestructorsOfReturn();
 
 	//jmp ...(End Sub/Function)
-	OpBuffer[obp++]=(char)0xE9;
-
-	pExitSubSchedule=(DWORD *)HeapReAlloc(hHeap,0,pExitSubSchedule,(ExitSubScheduleNum+1)*sizeof(DWORD));
-	pExitSubSchedule[ExitSubScheduleNum]=obp;
-	ExitSubScheduleNum++;
-
-	obp+=sizeof(long);
-}
-
-void AddCaseSchedule(void){
-	extern DWORD *pCaseSchedule;
-	extern int CaseScheduleNum;
-	extern HANDLE hHeap;
-
-	pCaseSchedule=(DWORD *)HeapReAlloc(hHeap,0,pCaseSchedule,(CaseScheduleNum+1)*sizeof(DWORD));
-	pCaseSchedule[CaseScheduleNum]=obp;
-	CaseScheduleNum++;
-}
+	compiler.codeGenerator.op_jmp_exitsub();
+}
+
+//Caseスケジュール
+class SelectSchedule
+{
+public:
+	SelectSchedule( int typeSize )
+		: typeSize( typeSize )
+		, nowCaseSchedule( 0 )
+	{
+	}
+
+	PertialSchedules casePertialSchedules;
+	int typeSize;
+	int nowCaseSchedule;
+};
+std::vector<SelectSchedule> selectSchedules;
+
 void OpcodeSelect( const char *lpszParms ){
-	extern DWORD *pCaseSchedule;
-	extern int CaseScheduleNum;
-	extern int NowCaseSchedule;
 	extern HANDLE hHeap;
 	extern char *basbuf;
 	int i,i2,i3,NowCaseCp;
 	char temporary[VN_SIZE];
-
-	DWORD *temp_pCaseSchedule;
-	int temp_CaseScheduleNum;
-	int temp_NowCaseSchedule;
-
-	temp_pCaseSchedule=pCaseSchedule;
-	temp_CaseScheduleNum=CaseScheduleNum;
-	temp_NowCaseSchedule=NowCaseSchedule;
-	pCaseSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
-	CaseScheduleNum=0;
-	NowCaseSchedule=0;
-
+	
 	int reg1=REG_RAX;
 	Type type1;
@@ -596,4 +576,10 @@
 	}
 
+	selectSchedules.push_back( SelectSchedule( type1.GetSize() ) );
+
+	if( selectSchedules.back().typeSize < sizeof(long) ){
+		selectSchedules.back().typeSize = sizeof(long);
+	}
+
 	if(type1.IsDouble()){
 		//movsd qword ptr[rsp+offset],xmm_reg		※スタックフレームを利用
@@ -613,8 +599,5 @@
 	for(i=cp;;i++){
 		if(basbuf[i]=='\0'){
-			HeapDefaultFree(pCaseSchedule);
-			pCaseSchedule=temp_pCaseSchedule;
-			CaseScheduleNum=temp_CaseScheduleNum;
-			NowCaseSchedule=temp_NowCaseSchedule;
+			selectSchedules.pop_back();
 			SetError(22,"Select",cp);
 			return;
@@ -718,6 +701,7 @@
 
 					//jne ...
-					OpBuffer[obp++]=(char)0x0F;
-					OpBuffer[obp++]=(char)0x85;
+					selectSchedules.back().casePertialSchedules.push_back(
+						compiler.codeGenerator.op_jne( 0, sizeof(long), true )
+					);
 				}
 				else{
@@ -759,9 +743,8 @@
 
 					//je ...
-					OpBuffer[obp++]=(char)0x0F;
-					OpBuffer[obp++]=(char)0x84;
+					selectSchedules.back().casePertialSchedules.push_back(
+						compiler.codeGenerator.op_je( 0, sizeof(long), true )
+					);
 				}
-				AddCaseSchedule();
-				obp+=sizeof(long);
 
 				if(basbuf[i]!=',') break;
@@ -770,7 +753,7 @@
 		if(basbuf[i]==1&&basbuf[i+1]==ESC_CASEELSE){
 			//jmp ...
-			OpBuffer[obp++]=(char)0xE9;
-			AddCaseSchedule();
-			obp+=sizeof(long);
+			selectSchedules.back().casePertialSchedules.push_back(
+				compiler.codeGenerator.op_jmp( 0, sizeof(long), true )
+			);
 		}
 	}
@@ -780,5 +763,5 @@
 
 	//レキシカルスコープをレベルアップ
-	GetLexicalScopes().Start( obp, SCOPE_TYPE_SELECT );
+	compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_SELECT );
 
 	//Select Case内をコンパイル
@@ -786,27 +769,22 @@
 
 	//jmp EndSelect
-	OpBuffer[obp++]=(char)0xE9;
-	AddCaseSchedule();
-	obp+=sizeof(long);
+	selectSchedules.back().casePertialSchedules.push_back(
+		compiler.codeGenerator.op_jmp( 0, sizeof(long), true )
+	);
 
 	//最終スケジュール
-	for(i=NowCaseSchedule;i<CaseScheduleNum;i++){
-		*(long *)(OpBuffer+pCaseSchedule[i])=obp-(pCaseSchedule[i]+sizeof(long));
-	}
-	HeapDefaultFree(pCaseSchedule);
+	for(i=selectSchedules.back().nowCaseSchedule;i<(int)selectSchedules.back().casePertialSchedules.size();i++){
+		compiler.codeGenerator.opfix_JmpPertialSchedule( selectSchedules.back().casePertialSchedules[i] );
+	}
 
 	//レキシカルスコープをレベルダウン
-	GetLexicalScopes().End();
-
-	pCaseSchedule=temp_pCaseSchedule;
-	CaseScheduleNum=temp_CaseScheduleNum;
-	NowCaseSchedule=temp_NowCaseSchedule;
+	compiler.codeGenerator.lexicalScopes.End();
+
+	selectSchedules.pop_back();
 }
 void OpcodeCase(char *Parameter){
-	extern DWORD *pCaseSchedule;
-	extern int NowCaseSchedule;
 	int i;
 
-	if(!pCaseSchedule){
+	if(selectSchedules.back().typeSize==-1){
 		SetError(30,"Case",cp);
 		return;
@@ -814,13 +792,13 @@
 
 	//jmp EndSelect
-	OpBuffer[obp++]=(char)0xE9;
-	AddCaseSchedule();
-	obp+=sizeof(long);
+	selectSchedules.back().casePertialSchedules.push_back(
+		compiler.codeGenerator.op_jmp( 0, sizeof(long), true )
+	);
 
 	i=0;
 	while(1){
 		//Caseスケジュール
-		*(long *)(OpBuffer+pCaseSchedule[NowCaseSchedule])=obp-(pCaseSchedule[NowCaseSchedule]+sizeof(long));
-		NowCaseSchedule++;
+		compiler.codeGenerator.opfix_JmpPertialSchedule( selectSchedules.back().casePertialSchedules[selectSchedules.back().nowCaseSchedule] );
+		selectSchedules.back().nowCaseSchedule++;
 
 		i=JumpOneParameter(Parameter,i);
@@ -830,62 +808,4 @@
 
 void OpcodeGosub(char *Parameter){
-	extern HANDLE hHeap;
-	extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
-	extern int GotoLabelScheduleNum;
-	int i,LineNum;
-
-	//call _System_GetEip
-	extern const UserProc *pSub_System_GetEip;
-	compiler.codeGenerator.op_call(pSub_System_GetEip);
-
-	//add rax,offset（Gosubステートメントの最終ポイント）
-	int schedule=obp,schedule2;
-	compiler.codeGenerator.op_add_RV(REG_RAX,0);
-	schedule2=obp-sizeof(long);
-
-	//※戻り先用のrip
-	//mov qword ptr[rsp+offset],rax     ※スタックフレームを利用
-	pobj_sf->push(REG_RAX);
-
-
-	if(Parameter[0]=='*'){
-		i=GetLabelAddress(Parameter+1,0);
-
-		//jmp ...
-		OpBuffer[obp++]=(char)0xE9;
-		if(i==-1){
-			pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapReAlloc(hHeap,0,pGotoLabelSchedule,(GotoLabelScheduleNum+1)*sizeof(GOTOLABELSCHEDULE));
-			pGotoLabelSchedule[GotoLabelScheduleNum].pName=(char *)HeapAlloc(hHeap,0,lstrlen(Parameter+1)+1);
-			lstrcpy(pGotoLabelSchedule[GotoLabelScheduleNum].pName,Parameter+1);
-			pGotoLabelSchedule[GotoLabelScheduleNum].pos=obp;
-			pGotoLabelSchedule[GotoLabelScheduleNum].now_cp=cp;
-			GotoLabelScheduleNum++;
-		}
-		*((long *)(OpBuffer+obp))=i-(obp+sizeof(long));
-		obp+=sizeof(long);
-	}
-	else{
-		LineNum=atoi(Parameter);
-		i=GetLabelAddress(0,LineNum);
-
-		//jmp ...
-		OpBuffer[obp++]=(char)0xE9;
-		if(i==-1){
-			pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapReAlloc(hHeap,0,pGotoLabelSchedule,(GotoLabelScheduleNum+1)*sizeof(GOTOLABELSCHEDULE));
-			pGotoLabelSchedule[GotoLabelScheduleNum].pName=0;
-			pGotoLabelSchedule[GotoLabelScheduleNum].line=LineNum;
-			pGotoLabelSchedule[GotoLabelScheduleNum].pos=obp;
-			pGotoLabelSchedule[GotoLabelScheduleNum].now_cp=cp;
-			GotoLabelScheduleNum++;
-		}
-		*((long *)(OpBuffer+obp))=i-(obp+sizeof(long));
-		obp+=sizeof(long);
-	}
-
-	*((long *)(OpBuffer+schedule2))=obp-schedule;
-
-	//※スタックフレームを元に戻す
-	pobj_sf->pop(REG_NON);
-
 	SetError(-1,"Gosub ～ Returnステートメントは64ビットコンパイラで利用することはできません。",cp);
 }
