| [206] | 1 | #include "stdafx.h"
 | 
|---|
 | 2 | 
 | 
|---|
| [183] | 3 | #include <jenga/include/smoothie/Smoothie.h>
 | 
|---|
 | 4 | #include <jenga/include/smoothie/LexicalAnalysis.h>
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | #include <Program.h>
 | 
|---|
| [193] | 7 | #include <Compiler.h>
 | 
|---|
| [183] | 8 | #include <LexicalScopingImpl.h>
 | 
|---|
| [206] | 9 | #include <Class.h>
 | 
|---|
 | 10 | #include <Variable.h>
 | 
|---|
| [195] | 11 | #include <NamespaceSupporter.h>
 | 
|---|
| [183] | 12 | 
 | 
|---|
| [3] | 13 | #include "../BasicCompiler_Common/common.h"
 | 
|---|
 | 14 | #include "Opcode.h"
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | 
 | 
|---|
| [86] | 17 | void SystemProc( const UserProc &userProc ){
 | 
|---|
 | 18 |     if( userProc.GetName() == "_System_GetEip" ){
 | 
|---|
| [3] | 19 |         //mov eax,dword ptr[esp]
 | 
|---|
| [235] | 20 |         compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_ESP, 0, MOD_BASE );
 | 
|---|
| [3] | 21 | 
 | 
|---|
 | 22 |         //ret
 | 
|---|
| [225] | 23 |         compiler.codeGenerator.op_ret();
 | 
|---|
| [3] | 24 |     }
 | 
|---|
| [86] | 25 |     else if( userProc.GetName() == "_System_InitDllGlobalVariables" ){
 | 
|---|
| [3] | 26 |         ////////////////////////////////////////
 | 
|---|
 | 27 |         // DLLのグローバル領域をコンパイル
 | 
|---|
 | 28 |         ////////////////////////////////////////
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 |         extern BOOL bDll;
 | 
|---|
 | 31 |         if(!bDll){
 | 
|---|
 | 32 |             //ret
 | 
|---|
| [225] | 33 |             compiler.codeGenerator.op_ret();
 | 
|---|
| [3] | 34 | 
 | 
|---|
 | 35 |             return;
 | 
|---|
 | 36 |         }
 | 
|---|
 | 37 | 
 | 
|---|
| [206] | 38 |         const UserProc *pBackUserProc;
 | 
|---|
| [76] | 39 |         pBackUserProc = &UserProc::CompilingUserProc();
 | 
|---|
 | 40 |         UserProc::CompileStartForGlobalArea();
 | 
|---|
| [3] | 41 | 
 | 
|---|
 | 42 |         int BackCp;
 | 
|---|
 | 43 |         BackCp=cp;
 | 
|---|
 | 44 |         cp=-1;
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 |         extern BOOL bDebugCompile;
 | 
|---|
 | 47 |         if(bDebugCompile){
 | 
|---|
 | 48 |             //デバッグ用の変数を定義
 | 
|---|
 | 49 |             DebugVariable();
 | 
|---|
 | 50 |         }
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |         //GC用の変数を定義
 | 
|---|
 | 53 |         InitGCVariables();
 | 
|---|
 | 54 | 
 | 
|---|
| [129] | 55 |         //_System_StartupProgramの呼び出し
 | 
|---|
| [206] | 56 |         extern const UserProc *pSub_System_StartupProgram;
 | 
|---|
| [225] | 57 |         compiler.codeGenerator.op_call(pSub_System_StartupProgram);
 | 
|---|
| [129] | 58 | 
 | 
|---|
| [42] | 59 |         //クラスに属する静的メンバを定義
 | 
|---|
| [193] | 60 |         compiler.GetMeta().GetClasses().InitStaticMember();
 | 
|---|
| [42] | 61 | 
 | 
|---|
| [3] | 62 |         GetGlobalDataForDll();
 | 
|---|
 | 63 | 
 | 
|---|
| [76] | 64 |         UserProc::CompileStartForUserProc( pBackUserProc );
 | 
|---|
| [3] | 65 |         cp=BackCp;
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |         //ret
 | 
|---|
| [225] | 68 |         compiler.codeGenerator.op_ret();
 | 
|---|
| [3] | 69 |     }
 | 
|---|
| [86] | 70 |     else if( userProc.GetName() == "_System_InitStaticLocalVariables" ){
 | 
|---|
| [3] | 71 |         //静的ローカルオブジェクトのコンストラクタ呼び出し
 | 
|---|
 | 72 | 
 | 
|---|
| [206] | 73 |         BOOST_FOREACH( Variable *pVar, compiler.GetMeta().GetGlobalVars() ){
 | 
|---|
| [76] | 74 |             if(memicmp(pVar->GetName().c_str(),"Static%",7)==0){
 | 
|---|
| [3] | 75 |                 //コンストラクタ呼び出し
 | 
|---|
| [206] | 76 |                 if( pVar->GetType().IsObject() ){
 | 
|---|
| [3] | 77 | 
 | 
|---|
 | 78 |                     //エラー用
 | 
|---|
| [76] | 79 |                     cp=pVar->source_code_address;
 | 
|---|
| [3] | 80 | 
 | 
|---|
| [50] | 81 |                     CallConstructor(
 | 
|---|
| [76] | 82 |                         pVar->GetName().c_str(),
 | 
|---|
| [206] | 83 |                         pVar->GetSubscripts(),
 | 
|---|
 | 84 |                         pVar->GetType(),
 | 
|---|
 | 85 |                         pVar->GetParamStrForConstructor().c_str());
 | 
|---|
| [3] | 86 |                 }
 | 
|---|
 | 87 |             }
 | 
|---|
 | 88 |         }
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 |         //ret
 | 
|---|
| [225] | 91 |         compiler.codeGenerator.op_ret();
 | 
|---|
| [3] | 92 |     }
 | 
|---|
| [86] | 93 |     else if( userProc.GetName() == "_System_Call_Destructor_of_GlobalObject" ){
 | 
|---|
| [3] | 94 | 
 | 
|---|
| [206] | 95 |         const UserProc *pBackUserProc;
 | 
|---|
| [76] | 96 |         pBackUserProc = &UserProc::CompilingUserProc();
 | 
|---|
 | 97 |         UserProc::CompileStartForGlobalArea();
 | 
|---|
| [3] | 98 | 
 | 
|---|
| [183] | 99 |         GetLexicalScopes().CallDestructorsOfScopeEnd();
 | 
|---|
| [3] | 100 | 
 | 
|---|
| [76] | 101 |         UserProc::CompileStartForUserProc( pBackUserProc );
 | 
|---|
| [3] | 102 | 
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |         //ret
 | 
|---|
| [225] | 105 |         compiler.codeGenerator.op_ret();
 | 
|---|
| [3] | 106 |     }
 | 
|---|
| [86] | 107 |     else if( userProc.GetName() == "_System_GetSp" ){
 | 
|---|
| [3] | 108 |         //mov eax,esp
 | 
|---|
| [225] | 109 |         compiler.codeGenerator.op_mov_RR(REG_EAX,REG_ESP);
 | 
|---|
| [3] | 110 | 
 | 
|---|
 | 111 |         //add eax,PTR_SIZE
 | 
|---|
| [225] | 112 |         compiler.codeGenerator.op_add_RV8(REG_EAX,PTR_SIZE);
 | 
|---|
| [3] | 113 | 
 | 
|---|
 | 114 |         //ret
 | 
|---|
| [225] | 115 |         compiler.codeGenerator.op_ret();
 | 
|---|
| [3] | 116 |     }
 | 
|---|
| [86] | 117 |     else if( userProc.GetName() == "_allrem" ){
 | 
|---|
| [3] | 118 |         //乗除演算用の特殊関数(64ビット整数対応)
 | 
|---|
 | 119 |         BYTE Buffer_allrem[]={
 | 
|---|
 | 120 |             0x53,0x57,0x33,0xFF,0x8B,0x44,0x24,0x10,0x0B,0xC0,0x7D,0x14,0x47,0x8B,0x54,0x24,0x0C,0xF7,0xD8,0xF7,0xDA,0x83,0xD8,0x00,0x89,0x44,0x24,0x10,0x89,0x54,0x24,0x0C,0x8B,0x44,0x24,0x18,0x0B,0xC0,0x7D,0x13,0x8B,0x54,0x24,0x14,0xF7,0xD8,0xF7,0xDA,0x83,0xD8,0x00,0x89,0x44,0x24,0x18,0x89,0x54,0x24,0x14,0x0B,0xC0,0x75,0x1B,0x8B,0x4C,0x24,0x14,0x8B,0x44,0x24,0x10,0x33,0xD2,0xF7,0xF1,0x8B,0x44,0x24,0x0C,0xF7,0xF1,0x8B,0xC2,0x33,0xD2,0x4F,0x79,0x4E,0xEB,0x53,0x8B,0xD8,0x8B,0x4C,0x24,0x14,0x8B,0x54,0x24,0x10,0x8B,0x44,0x24,0x0C,0xD1,0xEB,0xD1,0xD9,0xD1,0xEA,0xD1,0xD8,0x0B,0xDB,0x75,0xF4,0xF7,0xF1,0x8B,0xC8,0xF7,0x64,0x24,0x18,0x91,0xF7,0x64,0x24,0x14,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x10,0x77,0x08,0x72,0x0E,0x3B,0x44,0x24,0x0C,0x76,0x08,0x2B,0x44,0x24,0x14,0x1B,0x54,0x24,0x18,0x2B,0x44,0x24,0x0C,0x1B,0x54,0x24,0x10,0x4F,0x79,0x07,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,0x00,0x5F,0x5B,0xC2,0x10,0x00
 | 
|---|
 | 121 |         };
 | 
|---|
 | 122 | 
 | 
|---|
 | 123 |         memcpy(OpBuffer+obp,Buffer_allrem,178);
 | 
|---|
 | 124 |         obp+=178;
 | 
|---|
 | 125 |     }
 | 
|---|
| [86] | 126 |     else if( userProc.GetName() == "_aullrem" ){
 | 
|---|
| [3] | 127 |         //乗除演算用の特殊関数(64ビット整数対応)
 | 
|---|
 | 128 |         BYTE Buffer_aullrem[]={
 | 
|---|
 | 129 |             0x53,0x8B,0x44,0x24,0x14,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x10,0x8B,
 | 
|---|
 | 130 |             0x44,0x24,0x0C,0x33,0xD2,0xF7,0xF1,0x8B,0x44,0x24,0x08,0xF7,0xF1,0x8B,
 | 
|---|
 | 131 |             0xC2,0x33,0xD2,0xEB,0x50,0x8B,0xC8,0x8B,0x5C,0x24,0x10,0x8B,0x54,0x24,
 | 
|---|
 | 132 |             0x0C,0x8B,0x44,0x24,0x08,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,0x0B,
 | 
|---|
 | 133 |             0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xC8,0xF7,0x64,0x24,0x14,0x91,0xF7,0x64,
 | 
|---|
 | 134 |             0x24,0x10,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x0C,0x77,0x08,0x72,0x0E,
 | 
|---|
 | 135 |             0x3B,0x44,0x24,0x08,0x76,0x08,0x2B,0x44,0x24,0x10,0x1B,0x54,0x24,0x14,
 | 
|---|
 | 136 |             0x2B,0x44,0x24,0x08,0x1B,0x54,0x24,0x0C,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,
 | 
|---|
 | 137 |             0x00,0x5B,0xC2,0x10,0x00
 | 
|---|
 | 138 |         };
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 |         memcpy(OpBuffer+obp,Buffer_aullrem,117);
 | 
|---|
 | 141 |         obp+=117;
 | 
|---|
 | 142 |     }
 | 
|---|
| [86] | 143 |     else if( userProc.GetName() == "_allmul" ){
 | 
|---|
| [3] | 144 |         //乗算用の特殊関数(64ビット整数対応)
 | 
|---|
 | 145 |         BYTE Buffer_allmul[]={
 | 
|---|
 | 146 |             0x8B,0x44,0x24,0x08,0x8B,0x4C,0x24,0x10,0x0B,0xC8,0x8B,0x4C,0x24,0x0C,0x75,0x09,0x8B,0x44,0x24,0x04,0xF7,0xE1,0xC2,0x10,0x00,0x53,0xF7,0xE1,0x8B,0xD8,0x8B,0x44,0x24,0x08,0xF7,0x64,0x24,0x14,0x03,0xD8,0x8B,0x44,0x24,0x08,0xF7,0xE1,0x03,0xD3,0x5B,0xC2,0x10,0x00
 | 
|---|
 | 147 |         };
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 |         memcpy(OpBuffer+obp,Buffer_allmul,52);
 | 
|---|
 | 150 |         obp+=52;
 | 
|---|
 | 151 |     }
 | 
|---|
| [86] | 152 |     else if( userProc.GetName() == "_alldiv" ){
 | 
|---|
| [3] | 153 |         //除算用の特殊関数(64ビット整数対応)
 | 
|---|
 | 154 |         BYTE Buffer_alldiv[]={
 | 
|---|
 | 155 |             0x57,0x56,0x53,0x33,0xFF,0x8B,0x44,0x24,0x14,0x0B,0xC0,0x7D,0x14,0x47,0x8B,0x54,0x24,0x10,0xF7,0xD8,0xF7,0xDA,0x83,0xD8,0x00,0x89,0x44,0x24,0x14,0x89,0x54,0x24,0x10,0x8B,0x44,0x24,0x1C,0x0B,0xC0,0x7D,0x14,0x47,0x8B,0x54,0x24,0x18,0xF7,0xD8,0xF7,0xDA,0x83,0xD8,0x00,0x89,0x44,0x24,0x1C,0x89,0x54,0x24,0x18,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x18,0x8B,0x44,0x24,0x14,0x33,0xD2,0xF7,0xF1,0x8B,0xD8,0x8B,0x44,0x24,0x10,0xF7,0xF1,0x8B,0xD3,0xEB,0x41,0x8B,0xD8,0x8B,0x4C,0x24,0x18,0x8B,0x54,0x24,0x14,0x8B,0x44,0x24,0x10,0xD1,0xEB,0xD1,0xD9,0xD1,0xEA,0xD1,0xD8,0x0B,0xDB,0x75,0xF4,0xF7,0xF1,0x8B,0xF0,0xF7,0x64,0x24,0x1C,0x8B,0xC8,0x8B,0x44,0x24,0x18,0xF7,0xE6,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x14,0x77,0x08,0x72,0x07,0x3B,0x44,0x24,0x10,0x76,0x01,0x4E,0x33,0xD2,0x8B,0xC6,0x4F,0x75,0x07,0xF7,0xDA,0xF7,0xD8,0x83,0xDA,0x00,0x5B,0x5E,0x5F,0xC2,0x10,0x00
 | 
|---|
 | 156 |         };
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 |         memcpy(OpBuffer+obp,Buffer_alldiv,170);
 | 
|---|
 | 159 |         obp+=170;
 | 
|---|
 | 160 |     }
 | 
|---|
| [86] | 161 |     else if( userProc.GetName() == "_aulldiv" ){
 | 
|---|
| [3] | 162 |         //整数除算用の特殊関数(64ビット整数対応)
 | 
|---|
 | 163 |         BYTE Buffer_aulldiv[]={
 | 
|---|
 | 164 |             0x53,0x56,0x8B,0x44,0x24,0x18,0x0B,0xC0,0x75,0x18,0x8B,0x4C,0x24,0x14,
 | 
|---|
 | 165 |             0x8B,0x44,0x24,0x10,0x33,0xD2,0xF7,0xF1,0x8B,0xD8,0x8B,0x44,0x24,0x0C,
 | 
|---|
 | 166 |             0xF7,0xF1,0x8B,0xD3,0xEB,0x41,0x8B,0xC8,0x8B,0x5C,0x24,0x14,0x8B,0x54,
 | 
|---|
 | 167 |             0x24,0x10,0x8B,0x44,0x24,0x0C,0xD1,0xE9,0xD1,0xDB,0xD1,0xEA,0xD1,0xD8,
 | 
|---|
 | 168 |             0x0B,0xC9,0x75,0xF4,0xF7,0xF3,0x8B,0xF0,0xF7,0x64,0x24,0x18,0x8B,0xC8,
 | 
|---|
 | 169 |             0x8B,0x44,0x24,0x14,0xF7,0xE6,0x03,0xD1,0x72,0x0E,0x3B,0x54,0x24,0x10,
 | 
|---|
 | 170 |             0x77,0x08,0x72,0x07,0x3B,0x44,0x24,0x0C,0x76,0x01,0x4E,0x33,0xD2,0x8B,
 | 
|---|
 | 171 |             0xC6,0x5E,0x5B,0xC2,0x10,0x00
 | 
|---|
 | 172 |         };
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 |         memcpy(OpBuffer+obp,Buffer_aulldiv,104);
 | 
|---|
 | 175 |         obp+=104;
 | 
|---|
 | 176 |     }
 | 
|---|
| [86] | 177 |     else if( userProc.GetName() == "_allshl" ){
 | 
|---|
| [3] | 178 |         //符号あり左ビットシフト用の特殊関数(64ビット整数対応)
 | 
|---|
 | 179 |         BYTE Buffer_allshl[]={
 | 
|---|
 | 180 |             0x80,0xF9,0x40,0x73,0x15,0x80,0xF9,0x20,0x73,0x06,0x0F,0xA5,0xC2,0xD3,0xE0,0xC3,0x8B,0xD0,0x33,0xC0,0x80,0xE1,0x1F,0xD3,0xE2,0xC3,0x33,0xC0,0x33,0xD2,0xC3
 | 
|---|
 | 181 |         };
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 |         memcpy(OpBuffer+obp,Buffer_allshl,31);
 | 
|---|
 | 184 |         obp+=31;
 | 
|---|
 | 185 |     }
 | 
|---|
| [86] | 186 |     else if( userProc.GetName() == "_allshr" ){
 | 
|---|
| [3] | 187 |         //符号あり右ビットシフト用の特殊関数(64ビット整数対応)
 | 
|---|
 | 188 |         BYTE Buffer_allshr[]={
 | 
|---|
 | 189 |             0x80,0xF9,0x40,0x73,0x16,0x80,0xF9,0x20,0x73,0x06,0x0F,0xAD,0xD0,0xD3,0xFA,0xC3,0x8B,0xC2,0xC1,0xFA,0x1F,0x80,0xE1,0x1F,0xD3,0xF8,0xC3,0xC1,0xFA,0x1F,0x8B,0xC2,0xC3
 | 
|---|
 | 190 |         };
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 |         memcpy(OpBuffer+obp,Buffer_allshr,33);
 | 
|---|
 | 193 |         obp+=33;
 | 
|---|
 | 194 |     }
 | 
|---|
| [86] | 195 |     else if( userProc.GetName() == "_aullshr" ){
 | 
|---|
| [3] | 196 |         //符号なし右ビットシフト用の特殊関数(64ビット整数対応)
 | 
|---|
 | 197 |         BYTE Buffer_aullshr[]={
 | 
|---|
 | 198 |             0x80,0xF9,0x40,         //cmp         cl,40h
 | 
|---|
 | 199 |             0x73,0x15,              //jae         RETZERO (0040d71a)
 | 
|---|
 | 200 |             0x80,0xF9,0x20,         //cmp         cl,20h
 | 
|---|
 | 201 |             0x73,0x06,              //jae         MORE32 (0040d710)
 | 
|---|
 | 202 |             0x0F,0xAD,0xD0,         //shrd        eax,edx,cl
 | 
|---|
 | 203 |             0xD3,0xEA,              //shr         edx,cl
 | 
|---|
 | 204 |             0xC3,                   //ret
 | 
|---|
 | 205 |             //MORE32:
 | 
|---|
 | 206 |             0x8B,0xC2,              //mov         eax,edx
 | 
|---|
 | 207 |             0x33,0xD2,              //xor         edx,edx
 | 
|---|
 | 208 |             0x80,0xE1,0x1F,         //and         cl,1Fh
 | 
|---|
 | 209 |             0xD3,0xE8,              //shr         eax,cl
 | 
|---|
 | 210 |             0xC3,                   //ret
 | 
|---|
 | 211 |             //RETZERO:
 | 
|---|
 | 212 |             0x33,0xC0,              //xor         eax,eax
 | 
|---|
 | 213 |             0x33,0xD2,              //xor         edx,edx
 | 
|---|
 | 214 |             0xC3                    //ret
 | 
|---|
 | 215 |         };
 | 
|---|
 | 216 | 
 | 
|---|
 | 217 |         memcpy(OpBuffer+obp,Buffer_aullshr,31);
 | 
|---|
 | 218 |         obp+=31;
 | 
|---|
 | 219 |     }
 | 
|---|
| [90] | 220 |     else{
 | 
|---|
 | 221 |         SetError();
 | 
|---|
 | 222 |     }
 | 
|---|
 | 223 | }
 | 
|---|
| [206] | 224 | void AutoGeneration( const UserProc &userProc){
 | 
|---|
| [90] | 225 |     if( userProc.GetName() == "InitializeUserTypes"
 | 
|---|
| [89] | 226 |         && userProc.HasParentClass()
 | 
|---|
| [131] | 227 |         && userProc.GetParentClass().GetName() == "_System_TypeBase" ){
 | 
|---|
| [89] | 228 | 
 | 
|---|
| [193] | 229 |             compiler.GetMeta().GetClasses().Compile_System_InitializeUserTypes();
 | 
|---|
| [86] | 230 |     }
 | 
|---|
| [95] | 231 |     else if( userProc.GetName() == "RegisterGlobalRoots"
 | 
|---|
 | 232 |         && userProc.HasParentClass()
 | 
|---|
| [131] | 233 |         && userProc.GetParentClass().GetName() == "_System_CGarbageCollection" ){
 | 
|---|
| [95] | 234 | 
 | 
|---|
 | 235 |             Compile_AddGlobalRootsForGc();
 | 
|---|
 | 236 |     }
 | 
|---|
| [78] | 237 |     else{
 | 
|---|
 | 238 |         SetError();
 | 
|---|
 | 239 |     }
 | 
|---|
| [3] | 240 | }
 | 
|---|
 | 241 | 
 | 
|---|
| [206] | 242 | void _compile_proc(const UserProc *pUserProc){
 | 
|---|
| [3] | 243 |     extern char *basbuf;
 | 
|---|
 | 244 |     extern HANDLE hHeap;
 | 
|---|
 | 245 |     extern BOOL bDebugCompile;
 | 
|---|
| [76] | 246 |     int i3,i4,LocalVarSchedule,EspOffsetSchedule,BaseOffset;
 | 
|---|
| [3] | 247 |     char temporary[VN_SIZE];
 | 
|---|
 | 248 | 
 | 
|---|
| [75] | 249 |     if( pUserProc->IsUsing() == false || pUserProc->IsCompiled() ) return;
 | 
|---|
| [3] | 250 | 
 | 
|---|
| [206] | 251 |     if( pUserProc->GetLocalVars().size() ){
 | 
|---|
| [76] | 252 |         SetError();
 | 
|---|
 | 253 |         return;
 | 
|---|
 | 254 |     }
 | 
|---|
 | 255 | 
 | 
|---|
| [206] | 256 |     trace_for_sourcecodestep( "★★★ " << pUserProc->GetFullName() << "のコンパイルを開始" );
 | 
|---|
 | 257 | 
 | 
|---|
| [75] | 258 |     pUserProc->CompleteCompile();
 | 
|---|
| [3] | 259 | 
 | 
|---|
 | 260 |     extern BOOL bSystemProc;
 | 
|---|
| [75] | 261 |     if(memcmp(pUserProc->GetName().c_str(),"_System_",8)==0) bSystemProc=1;
 | 
|---|
| [3] | 262 |     else bSystemProc=0;
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 |     extern BOOL bDebugSupportProc;
 | 
|---|
| [75] | 265 |     if(memcmp(pUserProc->GetName().c_str(),"_DebugSys_",10)==0){
 | 
|---|
| [3] | 266 |         if(!bDebugCompile){
 | 
|---|
 | 267 |             return;
 | 
|---|
 | 268 |         }
 | 
|---|
 | 269 |         bDebugSupportProc=1;
 | 
|---|
 | 270 |     }
 | 
|---|
 | 271 |     else bDebugSupportProc=0;
 | 
|---|
 | 272 | 
 | 
|---|
| [206] | 273 |     pUserProc->SetBeginOpAddress( obp );
 | 
|---|
| [3] | 274 | 
 | 
|---|
| [89] | 275 |     //コンパイル中の関数が属するクラス
 | 
|---|
| [206] | 276 |     compiler.pCompilingClass=pUserProc->GetParentClassPtr();
 | 
|---|
| [89] | 277 | 
 | 
|---|
 | 278 |     //コンパイルスタートをクラス管理クラスに追加
 | 
|---|
| [193] | 279 |     compiler.GetMeta().GetClasses().StartCompile( pUserProc );
 | 
|---|
| [89] | 280 | 
 | 
|---|
 | 281 |     //コンパイル中の関数
 | 
|---|
 | 282 |     UserProc::CompileStartForUserProc( pUserProc );
 | 
|---|
 | 283 | 
 | 
|---|
| [101] | 284 |     // コンパイル中の関数が属する名前空間
 | 
|---|
| [199] | 285 |     compiler.GetNamespaceSupporter().SetLivingNamespaceScopes( pUserProc->GetNamespaceScopes() );
 | 
|---|
| [101] | 286 | 
 | 
|---|
| [108] | 287 |     // コンパイル中の関数でImportsされている名前空間
 | 
|---|
| [199] | 288 |     compiler.GetNamespaceSupporter().SetImportedNamespaces( pUserProc->GetImportedNamespaces() );
 | 
|---|
| [108] | 289 | 
 | 
|---|
| [225] | 290 |     // コード生成対象を選択
 | 
|---|
 | 291 |     compiler.codeGenerator.Select( (const_cast<UserProc *>(pUserProc))->GetNativeCode() );
 | 
|---|
 | 292 | 
 | 
|---|
| [75] | 293 |     if(pUserProc->IsSystem()){
 | 
|---|
| [89] | 294 |         ////////////////////
 | 
|---|
 | 295 |         // 特殊関数
 | 
|---|
 | 296 |         ////////////////////
 | 
|---|
 | 297 | 
 | 
|---|
| [3] | 298 |         extern int AllLocalVarSize;
 | 
|---|
 | 299 |         AllLocalVarSize=0;
 | 
|---|
 | 300 | 
 | 
|---|
| [86] | 301 |         SystemProc(*pUserProc);
 | 
|---|
| [3] | 302 | 
 | 
|---|
| [206] | 303 |         pUserProc->SetEndOpAddress( obp );
 | 
|---|
| [3] | 304 |         return;
 | 
|---|
 | 305 |     }
 | 
|---|
 | 306 | 
 | 
|---|
| [75] | 307 |     cp=pUserProc->GetCodePos();
 | 
|---|
| [3] | 308 |     for(;;cp++){
 | 
|---|
 | 309 |         if(IsCommandDelimitation(basbuf[cp])) break;
 | 
|---|
 | 310 |     }
 | 
|---|
 | 311 |     cp--;
 | 
|---|
 | 312 | 
 | 
|---|
 | 313 |     //ローカル変数に関する情報
 | 
|---|
 | 314 |     extern int AllLocalVarSize;
 | 
|---|
 | 315 |     AllLocalVarSize=0;
 | 
|---|
 | 316 | 
 | 
|---|
 | 317 |     //ローカル変数アドレススケジュール
 | 
|---|
 | 318 |     extern DWORD *pLocalVarAddrSchedule;
 | 
|---|
 | 319 |     extern int LocalVarAddrScheduleNum;
 | 
|---|
 | 320 |     pLocalVarAddrSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
 | 
|---|
 | 321 |     LocalVarAddrScheduleNum=0;
 | 
|---|
 | 322 | 
 | 
|---|
 | 323 |     //パラメータ用の変数データを考慮
 | 
|---|
| [75] | 324 |     for(i3=(int)pUserProc->RealParams().size()-1;i3>=0;i3--){
 | 
|---|
 | 325 |         Parameter ¶m = *pUserProc->RealParams()[i3];
 | 
|---|
| [73] | 326 | 
 | 
|---|
| [206] | 327 |         Variable *pVar = new Variable( param.GetVarName(), param, false, param.IsRef(), "" );
 | 
|---|
| [3] | 328 | 
 | 
|---|
| [76] | 329 |         if( param.IsArray() ){
 | 
|---|
| [206] | 330 |             pVar->SetArray( param.GetSubscripts() );
 | 
|---|
| [3] | 331 |         }
 | 
|---|
 | 332 | 
 | 
|---|
| [76] | 333 |         int varSize;
 | 
|---|
| [73] | 334 |         if( param.IsRef() == false && param.IsStruct() ){
 | 
|---|
| [64] | 335 |             //構造体のByValパラメータ
 | 
|---|
| [76] | 336 |             pVar->ThisIsParameter();
 | 
|---|
 | 337 |             varSize=PTR_SIZE;
 | 
|---|
| [3] | 338 |         }
 | 
|---|
 | 339 |         else{
 | 
|---|
| [76] | 340 |             if( param.IsArray() == false ){
 | 
|---|
 | 341 |                 varSize = pVar->GetMemorySize();
 | 
|---|
| [3] | 342 |             }
 | 
|---|
 | 343 |             else{
 | 
|---|
| [76] | 344 |                 varSize=PTR_SIZE;
 | 
|---|
| [3] | 345 |             }
 | 
|---|
 | 346 |         }
 | 
|---|
| [76] | 347 |         AllLocalVarSize+=varSize;
 | 
|---|
| [206] | 348 |         pVar->SetOffsetAddress( AllLocalVarSize );
 | 
|---|
| [3] | 349 | 
 | 
|---|
 | 350 |         //レキシカルスコープ情報
 | 
|---|
| [206] | 351 |         pVar->SetScopeLevel( GetLexicalScopes().GetNowLevel() );
 | 
|---|
 | 352 |         pVar->SetScopeStartAddress( GetLexicalScopes().GetStartAddress() );
 | 
|---|
| [76] | 353 |         pVar->bLiving=TRUE;
 | 
|---|
| [3] | 354 | 
 | 
|---|
| [206] | 355 |         pUserProc->GetLocalVars().push_back( pVar );
 | 
|---|
| [3] | 356 |     }
 | 
|---|
 | 357 | 
 | 
|---|
 | 358 |     //Thisポインタを示すローカルオフセット値をセット
 | 
|---|
 | 359 |     extern int LocalVar_ThisPtrOffset;
 | 
|---|
 | 360 |     LocalVar_ThisPtrOffset=AllLocalVarSize;
 | 
|---|
 | 361 | 
 | 
|---|
 | 362 |     BaseOffset=AllLocalVarSize;
 | 
|---|
 | 363 | 
 | 
|---|
 | 364 |     //ret用のアドレスを考慮
 | 
|---|
 | 365 |     AllLocalVarSize+=sizeof(long);
 | 
|---|
 | 366 | 
 | 
|---|
 | 367 | 
 | 
|---|
 | 368 |     ///////////////////////
 | 
|---|
 | 369 |     // ここからコード生成
 | 
|---|
 | 370 | 
 | 
|---|
 | 371 |     //sub esp,AllLocalVarSize(スケジュール)
 | 
|---|
| [225] | 372 |     compiler.codeGenerator.op_sub_esp(0xFFFFFFFF);
 | 
|---|
| [3] | 373 |     LocalVarSchedule=obp-sizeof(long);
 | 
|---|
 | 374 | 
 | 
|---|
 | 375 |     //push ebp
 | 
|---|
| [225] | 376 |     compiler.codeGenerator.op_push(REG_EBP);
 | 
|---|
| [3] | 377 | 
 | 
|---|
 | 378 |     //mov ebp,esp
 | 
|---|
| [235] | 379 |     compiler.codeGenerator.op_mov_RR( REG_EBP, REG_ESP );
 | 
|---|
| [3] | 380 | 
 | 
|---|
 | 381 |     //push ebx
 | 
|---|
| [225] | 382 |     compiler.codeGenerator.op_push(REG_EBX);
 | 
|---|
| [3] | 383 | 
 | 
|---|
 | 384 |     //push esi
 | 
|---|
| [235] | 385 |     compiler.codeGenerator.op_push( REG_ESI );
 | 
|---|
| [3] | 386 | 
 | 
|---|
 | 387 |     //push edi
 | 
|---|
| [235] | 388 |     compiler.codeGenerator.op_push( REG_EDI );
 | 
|---|
| [3] | 389 | 
 | 
|---|
| [75] | 390 |     if( !pUserProc->ReturnType().IsNull() ){
 | 
|---|
| [3] | 391 |         //戻り値が存在するとき
 | 
|---|
 | 392 | 
 | 
|---|
| [75] | 393 |         const char *temp = pUserProc->GetName().c_str();
 | 
|---|
 | 394 |         if( temp[0]==1&&temp[1]==ESC_OPERATOR ){
 | 
|---|
 | 395 |             temp = "_System_ReturnValue";
 | 
|---|
 | 396 |         }
 | 
|---|
| [3] | 397 | 
 | 
|---|
| [75] | 398 |         if( pUserProc->ReturnType().IsStruct() ){
 | 
|---|
| [64] | 399 |             //戻り値用の構造体(値型)はパラメータで引き渡される
 | 
|---|
| [3] | 400 |         }
 | 
|---|
 | 401 |         else{
 | 
|---|
| [89] | 402 |             if( pUserProc->ReturnType().IsObject() ){
 | 
|---|
| [193] | 403 |                 sprintf(temporary,"%s=Nothing%c%c%s",temp,1,ESC_AS, Compiler::TypeToString( pUserProc->ReturnType() ).c_str() );
 | 
|---|
| [89] | 404 |             }
 | 
|---|
 | 405 |             else{
 | 
|---|
 | 406 |                 //戻り値用の変数の定義
 | 
|---|
| [193] | 407 |                 sprintf(temporary,"%s%c%c%s",temp,1,ESC_AS, Compiler::TypeToString( pUserProc->ReturnType() ).c_str() );
 | 
|---|
| [89] | 408 |             }
 | 
|---|
| [40] | 409 | 
 | 
|---|
| [3] | 410 |             OpcodeDim(temporary,0);
 | 
|---|
 | 411 |         }
 | 
|---|
 | 412 |     }
 | 
|---|
 | 413 | 
 | 
|---|
 | 414 |     //プロシージャ抜け出しスケジュール(Exit Sub/Function)
 | 
|---|
 | 415 |     extern DWORD *pExitSubSchedule;
 | 
|---|
 | 416 |     extern int ExitSubScheduleNum;
 | 
|---|
 | 417 |     pExitSubSchedule=(DWORD *)HeapAlloc(hHeap,0,1);
 | 
|---|
 | 418 |     ExitSubScheduleNum=0;
 | 
|---|
 | 419 | 
 | 
|---|
 | 420 |     //ラベル用のメモリを確保
 | 
|---|
 | 421 |     extern LABEL *pLabelNames;
 | 
|---|
 | 422 |     extern int MaxLabelNum;
 | 
|---|
 | 423 |     pLabelNames=(LABEL *)HeapAlloc(hHeap,0,1);
 | 
|---|
 | 424 |     MaxLabelNum=0;
 | 
|---|
 | 425 | 
 | 
|---|
 | 426 |     //Gotoラベルスケジュール
 | 
|---|
 | 427 |     extern GOTOLABELSCHEDULE *pGotoLabelSchedule;
 | 
|---|
 | 428 |     extern int GotoLabelScheduleNum;
 | 
|---|
 | 429 |     pGotoLabelSchedule=(GOTOLABELSCHEDULE *)HeapAlloc(hHeap,0,1);
 | 
|---|
 | 430 |     GotoLabelScheduleNum=0;
 | 
|---|
 | 431 | 
 | 
|---|
 | 432 |     //With情報のメモリを確保
 | 
|---|
 | 433 |     extern WITHINFO WithInfo;
 | 
|---|
 | 434 |     WithInfo.ppName=(char **)HeapAlloc(hHeap,0,1);
 | 
|---|
 | 435 |     WithInfo.pWithCp=(int *)HeapAlloc(hHeap,0,1);
 | 
|---|
 | 436 |     WithInfo.num=0;
 | 
|---|
 | 437 | 
 | 
|---|
 | 438 |     //重複エラー情報管理のメモリを確保
 | 
|---|
 | 439 |     extern char **SynonymErrorWords;
 | 
|---|
 | 440 |     extern int SynonymErrorNum;
 | 
|---|
 | 441 |     SynonymErrorNum=0;
 | 
|---|
 | 442 |     SynonymErrorWords=(char **)HeapAlloc(hHeap,0,1);
 | 
|---|
 | 443 | 
 | 
|---|
 | 444 |     //Continueアドレスを初期化
 | 
|---|
| [241] | 445 |     compiler.codeGenerator.ClearContinueArea();
 | 
|---|
| [3] | 446 | 
 | 
|---|
 | 447 |     if(bDebugCompile&&bDebugSupportProc==0){
 | 
|---|
 | 448 |         //push dword ptr[ebp+(AllLocalVarSize-BaseOffset)](スケジュール)
 | 
|---|
 | 449 |         OpBuffer[obp++]=(char)0xFF;
 | 
|---|
 | 450 |         OpBuffer[obp++]=(char)0xB5;
 | 
|---|
 | 451 |         EspOffsetSchedule=obp;
 | 
|---|
 | 452 |         obp+=sizeof(long);  
 | 
|---|
 | 453 | 
 | 
|---|
 | 454 |         //push dword ptr[ebp](以前のebp)
 | 
|---|
| [235] | 455 |         compiler.codeGenerator.op_push_M( REG_EBP );
 | 
|---|
| [3] | 456 | 
 | 
|---|
 | 457 |         //call _DebugSys_StartProc
 | 
|---|
| [206] | 458 |         extern const UserProc *pSub_DebugSys_StartProc;
 | 
|---|
| [225] | 459 |         compiler.codeGenerator.op_call(pSub_DebugSys_StartProc);
 | 
|---|
| [3] | 460 |     }
 | 
|---|
 | 461 | 
 | 
|---|
| [206] | 462 |     if(compiler.pCompilingClass){
 | 
|---|
 | 463 |         if( pUserProc->GetName() == compiler.pCompilingClass->GetName() ){
 | 
|---|
| [3] | 464 |             ////////////////////////////////////
 | 
|---|
 | 465 |             // コンストラクタをコンパイルするとき
 | 
|---|
 | 466 |             ////////////////////////////////////
 | 
|---|
 | 467 | 
 | 
|---|
| [17] | 468 |             //コンストラクタのコンパイル開始を通知
 | 
|---|
| [206] | 469 |             compiler.pCompilingClass->NotifyStartConstructorCompile();
 | 
|---|
| [17] | 470 | 
 | 
|---|
| [27] | 471 |             //基底クラスかどうかの識別
 | 
|---|
 | 472 |             //(継承元がインターフェイスの場合も基底クラスと見なす)
 | 
|---|
| [3] | 473 |             BOOL bThisIsSuperClass;
 | 
|---|
| [206] | 474 |             if( !compiler.pCompilingClass->HasSuperClass() ) bThisIsSuperClass=1;
 | 
|---|
 | 475 |             else if( compiler.pCompilingClass->GetSuperClass().GetConstructorMethod() == NULL ){
 | 
|---|
| [3] | 476 |                 //インターフェイスを継承したときはコンストラクタを持たない
 | 
|---|
 | 477 |                 bThisIsSuperClass=1;
 | 
|---|
 | 478 |             }
 | 
|---|
 | 479 |             else bThisIsSuperClass=0;
 | 
|---|
 | 480 | 
 | 
|---|
 | 481 |             if(!bThisIsSuperClass){
 | 
|---|
 | 482 |                 /* サブクラスコンストラクタをコンパイルしているときは、
 | 
|---|
| [27] | 483 |                     基底クラスのコンストラクタを呼び出す */
 | 
|---|
| [3] | 484 | 
 | 
|---|
 | 485 |                 i3=cp+1;
 | 
|---|
 | 486 |                 while(IsCommandDelimitation(basbuf[i3])) i3++;
 | 
|---|
 | 487 |                 for(i4=0;;i3++,i4++){
 | 
|---|
 | 488 |                     if(!IsVariableChar(basbuf[i3])){
 | 
|---|
 | 489 |                         temporary[i4]=0;
 | 
|---|
 | 490 |                         break;
 | 
|---|
 | 491 |                     }
 | 
|---|
 | 492 |                     temporary[i4]=basbuf[i3];
 | 
|---|
 | 493 |                 }
 | 
|---|
| [206] | 494 |                 if( compiler.pCompilingClass->GetSuperClass().GetName() == temporary ){
 | 
|---|
| [27] | 495 |                     //基底クラスのコンストラクタを呼び出す
 | 
|---|
| [3] | 496 |                     cp=i3;
 | 
|---|
 | 497 |                     for(i4=0;;cp++,i4++){
 | 
|---|
 | 498 |                         if(IsCommandDelimitation(basbuf[cp])){
 | 
|---|
 | 499 |                             temporary[i4]=0;
 | 
|---|
 | 500 |                             break;
 | 
|---|
 | 501 |                         }
 | 
|---|
 | 502 |                         temporary[i4]=basbuf[cp];
 | 
|---|
 | 503 |                     }
 | 
|---|
 | 504 |                     if(!(temporary[0]=='('&&temporary[lstrlen(temporary)-1]==')')){
 | 
|---|
 | 505 |                         SetError(1,NULL,cp);
 | 
|---|
 | 506 |                     }
 | 
|---|
 | 507 |                     RemoveStringPare(temporary);
 | 
|---|
 | 508 | 
 | 
|---|
| [89] | 509 |                     Type dummyType;
 | 
|---|
 | 510 |                     CallProc( PROC_DEFAULT
 | 
|---|
| [206] | 511 |                         , &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc()
 | 
|---|
 | 512 |                         , compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc().GetName().c_str()
 | 
|---|
| [89] | 513 |                         , temporary
 | 
|---|
 | 514 |                         , dummyType );
 | 
|---|
| [3] | 515 |                 }
 | 
|---|
 | 516 |                 else{
 | 
|---|
| [27] | 517 |                     //基底クラスのコンストラクタを暗黙的に呼び出す
 | 
|---|
| [3] | 518 |                     Opcode_CallProc("",
 | 
|---|
| [206] | 519 |                         &compiler.pCompilingClass->GetSuperClass().GetConstructorMethod()->GetUserProc(),
 | 
|---|
| [3] | 520 |                         0,
 | 
|---|
 | 521 |                         "",
 | 
|---|
 | 522 |                         0);
 | 
|---|
 | 523 |                 }
 | 
|---|
 | 524 |             }
 | 
|---|
 | 525 | 
 | 
|---|
 | 526 |             //仮想関数テーブルを初期化
 | 
|---|
| [206] | 527 |             if( compiler.pCompilingClass->IsExistVirtualFunctions()
 | 
|---|
 | 528 |                 && !compiler.pCompilingClass->IsAbstract() ){
 | 
|---|
| [28] | 529 |                     //関数テーブルに値をセット
 | 
|---|
| [206] | 530 |                     int offset = (int)compiler.pCompilingClass->GetVtblGlobalOffset();
 | 
|---|
| [3] | 531 | 
 | 
|---|
| [28] | 532 |                     //mov eax,offset
 | 
|---|
| [237] | 533 |                     compiler.codeGenerator.op_mov_RV( REG_EAX, offset, Schedule::DataTable );
 | 
|---|
| [3] | 534 | 
 | 
|---|
| [28] | 535 |                     //Thisポインタをecxにコピー
 | 
|---|
 | 536 |                     SetThisPtrToReg(REG_ECX);
 | 
|---|
| [3] | 537 | 
 | 
|---|
| [28] | 538 |                     //mov dword ptr[ecx],eax
 | 
|---|
| [225] | 539 |                     compiler.codeGenerator.op_mov_MR( sizeof(long), REG_EAX, REG_ECX, 0, MOD_BASE );
 | 
|---|
| [3] | 540 |             }
 | 
|---|
 | 541 |         }
 | 
|---|
| [75] | 542 |         else if( pUserProc->IsDestructor() ){
 | 
|---|
| [18] | 543 |             //デストラクタをコンパイルしたとき
 | 
|---|
 | 544 | 
 | 
|---|
 | 545 |             //デストラクタのコンパイル開始を通知
 | 
|---|
| [206] | 546 |             compiler.pCompilingClass->NotifyStartDestructorCompile();
 | 
|---|
| [18] | 547 |         }
 | 
|---|
| [3] | 548 |     }
 | 
|---|
 | 549 | 
 | 
|---|
 | 550 |     //////////////////////////////////////////
 | 
|---|
 | 551 |     //////////////////////////////////////////
 | 
|---|
 | 552 |     ////// プロシージャ内をコンパイル ////////
 | 
|---|
| [90] | 553 |     if( pUserProc->IsAutoGeneration() ){
 | 
|---|
 | 554 |         AutoGeneration( *pUserProc );
 | 
|---|
 | 555 |     }
 | 
|---|
| [75] | 556 |     else{
 | 
|---|
| [90] | 557 |         if(pUserProc->IsMacro()){
 | 
|---|
 | 558 |             CompileBuffer(ESC_ENDMACRO,0);
 | 
|---|
 | 559 |         }
 | 
|---|
 | 560 |         else{
 | 
|---|
 | 561 |             if(pUserProc->IsSub()){
 | 
|---|
 | 562 |                 CompileBuffer(ESC_ENDSUB,0);
 | 
|---|
 | 563 |             }
 | 
|---|
 | 564 |             else if(pUserProc->IsFunction()){
 | 
|---|
 | 565 |                 CompileBuffer(ESC_ENDFUNCTION,0);
 | 
|---|
 | 566 |             }
 | 
|---|
 | 567 |         }
 | 
|---|
| [75] | 568 |     }
 | 
|---|
| [3] | 569 |     //////////////////////////////////////////
 | 
|---|
 | 570 |     //////////////////////////////////////////
 | 
|---|
 | 571 | 
 | 
|---|
| [206] | 572 |     if( compiler.pCompilingClass ){
 | 
|---|
| [17] | 573 | 
 | 
|---|
| [206] | 574 |         if( compiler.pCompilingClass->IsCompilingConstructor() ){
 | 
|---|
| [17] | 575 |             // コンストラクタをコンパイルしていたとき
 | 
|---|
| [18] | 576 | 
 | 
|---|
| [17] | 577 |             // コンストラクタのコンパイルが完了したことを通知
 | 
|---|
| [206] | 578 |             compiler.pCompilingClass->NotifyFinishConstructorCompile();
 | 
|---|
| [17] | 579 |         }
 | 
|---|
| [75] | 580 |         else if( pUserProc->IsDestructor() ){
 | 
|---|
| [3] | 581 |             ////////////////////////////////////
 | 
|---|
 | 582 |             //デストラクタをコンパイルしたとき
 | 
|---|
 | 583 |             ////////////////////////////////////
 | 
|---|
 | 584 | 
 | 
|---|
| [18] | 585 |             // デストラクタのコンパイルが完了したことを通知
 | 
|---|
| [206] | 586 |             compiler.pCompilingClass->NotifyFinishDestructorCompile();
 | 
|---|
| [18] | 587 | 
 | 
|---|
| [206] | 588 |             if( compiler.pCompilingClass->HasSuperClass() ){
 | 
|---|
| [3] | 589 |                 /* サブクラスのデストラクタをコンパイルしているときは、
 | 
|---|
| [27] | 590 |                     基底クラスのデストラクタを呼び出す */
 | 
|---|
| [3] | 591 | 
 | 
|---|
| [206] | 592 |                 const CMethod *method = compiler.pCompilingClass->GetSuperClass().GetDestructorMethod();
 | 
|---|
| [51] | 593 |                 if( method ){
 | 
|---|
| [3] | 594 |                     Opcode_CallProc("",
 | 
|---|
| [206] | 595 |                         &method->GetUserProc(),
 | 
|---|
| [3] | 596 |                         0,
 | 
|---|
 | 597 |                         "",
 | 
|---|
 | 598 |                         0);
 | 
|---|
 | 599 |                 }
 | 
|---|
 | 600 |             }
 | 
|---|
 | 601 |         }
 | 
|---|
 | 602 |     }
 | 
|---|
 | 603 | 
 | 
|---|
 | 604 |     //ラベル用のメモリを解放
 | 
|---|
 | 605 |     for(i3=0;i3<MaxLabelNum;i3++){
 | 
|---|
 | 606 |         if(pLabelNames[i3].pName) HeapDefaultFree(pLabelNames[i3].pName);
 | 
|---|
 | 607 |     }
 | 
|---|
 | 608 |     HeapDefaultFree(pLabelNames);
 | 
|---|
 | 609 | 
 | 
|---|
 | 610 |     //Goto未知ラベルスケジュールを解放
 | 
|---|
 | 611 |     for(i3=0;i3<GotoLabelScheduleNum;i3++){
 | 
|---|
 | 612 |         if(pGotoLabelSchedule[i3].pName){
 | 
|---|
 | 613 |             SetError(6,pGotoLabelSchedule[i3].pName,pGotoLabelSchedule[i3].now_cp);
 | 
|---|
 | 614 |             HeapDefaultFree(pGotoLabelSchedule[i3].pName);
 | 
|---|
 | 615 |         }
 | 
|---|
 | 616 |         else{
 | 
|---|
 | 617 |             sprintf(temporary,"%d",pGotoLabelSchedule[i3].line);
 | 
|---|
 | 618 |             SetError(6,temporary,pGotoLabelSchedule[i3].now_cp);
 | 
|---|
 | 619 |         }
 | 
|---|
 | 620 |     }
 | 
|---|
 | 621 |     HeapDefaultFree(pGotoLabelSchedule);
 | 
|---|
 | 622 | 
 | 
|---|
 | 623 |     //With情報のメモリを解放
 | 
|---|
 | 624 |     for(i3=0;i3<WithInfo.num;i3++){
 | 
|---|
 | 625 |         SetError(22,"With",WithInfo.pWithCp[i3]);
 | 
|---|
 | 626 |         HeapDefaultFree(WithInfo.ppName[i3]);
 | 
|---|
 | 627 |     }
 | 
|---|
 | 628 |     HeapDefaultFree(WithInfo.ppName);
 | 
|---|
 | 629 |     HeapDefaultFree(WithInfo.pWithCp);
 | 
|---|
 | 630 | 
 | 
|---|
 | 631 |     //push ebp
 | 
|---|
 | 632 |     AllLocalVarSize+=sizeof(long);
 | 
|---|
 | 633 | 
 | 
|---|
 | 634 |     //ローカルオブジェクトの解放処理
 | 
|---|
| [183] | 635 |     GetLexicalScopes().CallDestructorsOfScopeEnd();
 | 
|---|
| [3] | 636 | 
 | 
|---|
| [34] | 637 |     //プロシージャ抜け出しスケジュール(Exit Sub/Function)
 | 
|---|
 | 638 |     for(i3=0;i3<ExitSubScheduleNum;i3++){
 | 
|---|
 | 639 |         *((long *)(OpBuffer+pExitSubSchedule[i3]))=obp-(pExitSubSchedule[i3]+sizeof(long));
 | 
|---|
 | 640 |     }
 | 
|---|
 | 641 |     HeapDefaultFree(pExitSubSchedule);
 | 
|---|
 | 642 | 
 | 
|---|
 | 643 |     if(bDebugCompile&&bDebugSupportProc==0){
 | 
|---|
| [64] | 644 |         *((long *)(OpBuffer+EspOffsetSchedule))=AllLocalVarSize-BaseOffset-sizeof(long);
 | 
|---|
| [34] | 645 | 
 | 
|---|
 | 646 |         //call _DebugSys_EndProc
 | 
|---|
| [206] | 647 |         extern const UserProc *pSub_DebugSys_EndProc;
 | 
|---|
| [225] | 648 |         compiler.codeGenerator.op_call(pSub_DebugSys_EndProc);
 | 
|---|
| [34] | 649 |     }
 | 
|---|
 | 650 | 
 | 
|---|
| [75] | 651 |     if( !pUserProc->ReturnType().IsNull() ){
 | 
|---|
| [3] | 652 |         //戻り値をeax、edxに設定
 | 
|---|
 | 653 |         RELATIVE_VAR RelativeVar;
 | 
|---|
 | 654 | 
 | 
|---|
| [75] | 655 |         const char *temp = pUserProc->GetName().c_str();
 | 
|---|
 | 656 |         if( temp[0]==1 && temp[1]==ESC_OPERATOR ){
 | 
|---|
| [3] | 657 |             temp="_System_ReturnValue";
 | 
|---|
| [75] | 658 |         }
 | 
|---|
| [76] | 659 |         GetVarOffsetReadWrite(temp,&RelativeVar,Type());
 | 
|---|
| [3] | 660 | 
 | 
|---|
| [75] | 661 |         i3=pUserProc->ReturnType().GetBasicType();
 | 
|---|
| [3] | 662 | 
 | 
|---|
| [64] | 663 |         if(i3==DEF_OBJECT || i3==DEF_STRUCT){
 | 
|---|
| [3] | 664 |             SetVarPtrToEax(&RelativeVar);
 | 
|---|
| [64] | 665 |             if( i3==DEF_OBJECT ){
 | 
|---|
 | 666 |                 //mov eax,dword ptr[eax]
 | 
|---|
| [225] | 667 |                 compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
 | 
|---|
| [64] | 668 |             }
 | 
|---|
| [3] | 669 |         }
 | 
|---|
| [231] | 670 |         else if( i3==DEF_DOUBLE
 | 
|---|
 | 671 |             || i3 == DEF_SINGLE )
 | 
|---|
 | 672 |         {
 | 
|---|
| [3] | 673 |             //fld qword ptr[ebp+offset]
 | 
|---|
| [231] | 674 |             compiler.codeGenerator.op_fld_base_offset( i3, REG_EBP, RelativeVar.offset, Schedule::LocalVar );
 | 
|---|
| [3] | 675 |         }
 | 
|---|
 | 676 |         else if(i3==DEF_INT64||i3==DEF_QWORD){
 | 
|---|
 | 677 |             //mov eax,dword ptr[ebp+offset]
 | 
|---|
| [231] | 678 |             compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::LocalVar );
 | 
|---|
| [3] | 679 | 
 | 
|---|
 | 680 |             //mov edx,dword ptr[ebp+offset+sizeof(long)]
 | 
|---|
| [231] | 681 |             compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EBP, RelativeVar.offset+sizeof(long), MOD_BASE_DISP32, Schedule::LocalVar );
 | 
|---|
| [3] | 682 |         }
 | 
|---|
 | 683 |         else if(i3==DEF_LONG||i3==DEF_DWORD||
 | 
|---|
 | 684 |             IsPtrType(i3)){
 | 
|---|
 | 685 |             //mov eax,dword ptr[ebp+offset]
 | 
|---|
| [231] | 686 |             compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::LocalVar );
 | 
|---|
| [3] | 687 |         }
 | 
|---|
| [183] | 688 |         else if(i3==DEF_INTEGER||i3==DEF_WORD || (Smoothie::IsUnicode()&&i3==DEF_CHAR)){
 | 
|---|
| [3] | 689 |             //xor eax,eax(eaxを0に初期化する)
 | 
|---|
| [225] | 690 |             compiler.codeGenerator.op_zero_reg(REG_EAX);
 | 
|---|
| [3] | 691 | 
 | 
|---|
 | 692 |             //mov ax,word ptr[ebp+offset]
 | 
|---|
| [231] | 693 |             compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::LocalVar );
 | 
|---|
| [3] | 694 |         }
 | 
|---|
| [183] | 695 |         else if(i3==DEF_SBYTE||i3==DEF_BYTE||i3==DEF_BOOLEAN || (Smoothie::IsUnicode()==false&&i3==DEF_CHAR)){
 | 
|---|
| [3] | 696 |             //xor eax,eax(eaxを0に初期化する)
 | 
|---|
| [225] | 697 |             compiler.codeGenerator.op_zero_reg(REG_EAX);
 | 
|---|
| [3] | 698 | 
 | 
|---|
 | 699 |             //mov al,byte ptr[ebp+offset]
 | 
|---|
| [231] | 700 |             compiler.codeGenerator.op_mov_RM( sizeof(char), REG_EAX, REG_EBP, RelativeVar.offset, MOD_BASE_DISP32, Schedule::LocalVar );
 | 
|---|
| [3] | 701 |         }
 | 
|---|
 | 702 |     }
 | 
|---|
 | 703 | 
 | 
|---|
 | 704 |     //ローカル変数アドレススケジュール
 | 
|---|
 | 705 |     for(i3=0;i3<LocalVarAddrScheduleNum;i3++){
 | 
|---|
 | 706 |         *((long *)(OpBuffer+pLocalVarAddrSchedule[i3]))+=AllLocalVarSize;
 | 
|---|
 | 707 |     }
 | 
|---|
 | 708 |     HeapDefaultFree(pLocalVarAddrSchedule);
 | 
|---|
| [206] | 709 |     BOOST_FOREACH( Variable *pVar, pUserProc->GetLocalVars() ){
 | 
|---|
| [76] | 710 |         //後にデバッグで利用する
 | 
|---|
| [206] | 711 |         pVar->SetOffsetAddress( AllLocalVarSize - pVar->GetOffsetAddress() );
 | 
|---|
| [3] | 712 |     }
 | 
|---|
 | 713 | 
 | 
|---|
 | 714 |     //push ebp、ret用のアドレスを考慮
 | 
|---|
 | 715 |     AllLocalVarSize-=sizeof(long)*2;
 | 
|---|
 | 716 | 
 | 
|---|
 | 717 |     //ローカル変数用メモリを確保するためのスケジュール(subコマンド)
 | 
|---|
 | 718 |     *((long *)(OpBuffer+LocalVarSchedule))=AllLocalVarSize-BaseOffset;
 | 
|---|
 | 719 | 
 | 
|---|
 | 720 |     //pop edi
 | 
|---|
| [235] | 721 |     compiler.codeGenerator.op_pop( REG_EDI );
 | 
|---|
| [3] | 722 | 
 | 
|---|
 | 723 |     //pop esi
 | 
|---|
| [235] | 724 |     compiler.codeGenerator.op_pop( REG_ESI );
 | 
|---|
| [3] | 725 | 
 | 
|---|
 | 726 |     //pop ebx
 | 
|---|
| [225] | 727 |     compiler.codeGenerator.op_pop(REG_EBX);
 | 
|---|
| [3] | 728 | 
 | 
|---|
| [64] | 729 |     if(bDebugCompile){
 | 
|---|
 | 730 |         //cmp esp,ebp
 | 
|---|
| [225] | 731 |         compiler.codeGenerator.op_cmp_RR( REG_ESP, REG_EBP );
 | 
|---|
| [64] | 732 | 
 | 
|---|
| [240] | 733 |         //je 6(次のcallとbreakpointを飛び越す)
 | 
|---|
 | 734 |         compiler.codeGenerator.op_je( 6 );
 | 
|---|
| [64] | 735 | 
 | 
|---|
 | 736 |         //call _esp_error
 | 
|---|
| [206] | 737 |         extern const UserProc *pSub_esp_error;
 | 
|---|
| [225] | 738 |         compiler.codeGenerator.op_call( pSub_esp_error );
 | 
|---|
| [64] | 739 | 
 | 
|---|
 | 740 |         breakpoint;
 | 
|---|
 | 741 |     }
 | 
|---|
 | 742 | 
 | 
|---|
| [3] | 743 |     //mov esp,ebp
 | 
|---|
| [235] | 744 |     compiler.codeGenerator.op_mov_RR( REG_ESP, REG_EBP );
 | 
|---|
| [3] | 745 | 
 | 
|---|
 | 746 |     //pop ebp
 | 
|---|
| [225] | 747 |     compiler.codeGenerator.op_pop(REG_EBP);
 | 
|---|
| [3] | 748 | 
 | 
|---|
 | 749 |     //add esp AllLocalVarSize
 | 
|---|
| [225] | 750 |     compiler.codeGenerator.op_add_esp(AllLocalVarSize-BaseOffset);
 | 
|---|
| [3] | 751 | 
 | 
|---|
| [75] | 752 |     if( BaseOffset==0 || pUserProc->IsCdecl() ){
 | 
|---|
| [142] | 753 |         //ret
 | 
|---|
| [225] | 754 |         compiler.codeGenerator.op_ret();
 | 
|---|
| [3] | 755 |     }
 | 
|---|
 | 756 |     else{
 | 
|---|
 | 757 |         //ret BaseOffset(パラメータ分のスタック領域を解放)
 | 
|---|
| [240] | 758 |         compiler.codeGenerator.op_ret( (_int16)BaseOffset );
 | 
|---|
| [3] | 759 |     }
 | 
|---|
 | 760 | 
 | 
|---|
| [76] | 761 | 
 | 
|---|
| [206] | 762 |     pUserProc->SetEndOpAddress( obp );
 | 
|---|
| [3] | 763 | 
 | 
|---|
| [75] | 764 | 
 | 
|---|
 | 765 |     //重複エラー情報管理のメモリを解放
 | 
|---|
 | 766 |     for(i3=0;i3<SynonymErrorNum;i3++) HeapDefaultFree(SynonymErrorWords[i3]);
 | 
|---|
 | 767 |     HeapDefaultFree(SynonymErrorWords);
 | 
|---|
 | 768 |     SynonymErrorWords=0;
 | 
|---|
 | 769 | 
 | 
|---|
 | 770 | 
 | 
|---|
| [3] | 771 |     //ローカル変数のネーム情報は後に解放する
 | 
|---|
 | 772 | }
 | 
|---|
| [91] | 773 | 
 | 
|---|
| [206] | 774 | void CompileBufferInProcedure( const UserProc &userProc ){
 | 
|---|
| [91] | 775 |     if( userProc.IsUsing() == false || userProc.IsCompiled() ) return;
 | 
|---|
 | 776 | 
 | 
|---|
 | 777 |     _compile_proc( &userProc );
 | 
|---|
| [92] | 778 | /*
 | 
|---|
| [91] | 779 |     // ログを履く
 | 
|---|
 | 780 |     char temporary[8192];
 | 
|---|
 | 781 |     temporary[0]=0;
 | 
|---|
 | 782 |     lstrcat( temporary, "------------------------------------------------------------------\n" );
 | 
|---|
 | 783 |     sprintf( temporary + lstrlen(temporary), "【 %s のコード情報】\n", userProc.GetFullName().c_str() );
 | 
|---|
 | 784 |     sprintf( temporary + lstrlen(temporary), "code size: %d bytes\n", userProc.GetCodeSize() );
 | 
|---|
 | 785 |     lstrcat( temporary, "------------------------------------------------------------------\n" );
 | 
|---|
 | 786 |     lstrcat( temporary, "\n" );
 | 
|---|
| [92] | 787 |     Smoothie::Logger::Put( temporary );*/
 | 
|---|
| [91] | 788 | }
 | 
|---|
| [3] | 789 | void CompileLocal(){
 | 
|---|
 | 790 |     extern BOOL bDll;
 | 
|---|
 | 791 |     if(bDll){
 | 
|---|
 | 792 |         //DLLの場合はグローバル変数を初期化するための関数を一番初めにコンパイルする
 | 
|---|
| [206] | 793 |         const UserProc *pUserProc=GetSubHash("_System_InitDllGlobalVariables");
 | 
|---|
| [75] | 794 |         if(pUserProc){
 | 
|---|
| [91] | 795 |             CompileBufferInProcedure( *pUserProc );
 | 
|---|
| [3] | 796 |         }
 | 
|---|
 | 797 |         else SetError(300,NULL,cp);
 | 
|---|
 | 798 |     }
 | 
|---|
 | 799 | 
 | 
|---|
| [94] | 800 |     //_System_TypeBase_InitializeUserTypesは一番最後にコンパイル
 | 
|---|
| [206] | 801 |     extern const UserProc *pSubStaticMethod_System_TypeBase_InitializeUserTypes;
 | 
|---|
| [94] | 802 |     pSubStaticMethod_System_TypeBase_InitializeUserTypes->CompleteCompile();
 | 
|---|
 | 803 | 
 | 
|---|
| [3] | 804 |     //_System_InitStaticLocalVariablesは一番最後にコンパイル
 | 
|---|
 | 805 |     //※一般関数内の静的変数オブジェクトをすべて収集しなければならない
 | 
|---|
| [206] | 806 |     extern const UserProc *pSub_System_InitStaticLocalVariables;
 | 
|---|
| [75] | 807 |     pSub_System_InitStaticLocalVariables->CompleteCompile();
 | 
|---|
| [3] | 808 | 
 | 
|---|
 | 809 |     //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
 | 
|---|
| [206] | 810 |     extern const UserProc *pSub_System_Call_Destructor_of_GlobalObject;
 | 
|---|
| [75] | 811 |     pSub_System_Call_Destructor_of_GlobalObject->CompleteCompile();
 | 
|---|
| [3] | 812 | 
 | 
|---|
| [94] | 813 | repeat:
 | 
|---|
| [206] | 814 |     compiler.GetMeta().GetUserProcs().Iterator_Reset();
 | 
|---|
 | 815 |     while( compiler.GetMeta().GetUserProcs().Iterator_HasNext() )
 | 
|---|
 | 816 |     {
 | 
|---|
 | 817 |         UserProc *pUserProc = compiler.GetMeta().GetUserProcs().Iterator_GetNext();
 | 
|---|
 | 818 |         CompileBufferInProcedure( *pUserProc );
 | 
|---|
| [3] | 819 |     }
 | 
|---|
 | 820 | 
 | 
|---|
| [94] | 821 |     if( IsNeedProcCompile() ){
 | 
|---|
 | 822 |         //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
 | 
|---|
 | 823 |         goto repeat;
 | 
|---|
 | 824 |     }
 | 
|---|
 | 825 | 
 | 
|---|
 | 826 |     //_System_TypeBase_InitializeUserTypesは最後のほうでコンパイル
 | 
|---|
 | 827 |     pSubStaticMethod_System_TypeBase_InitializeUserTypes->KillCompileStatus();
 | 
|---|
 | 828 |     CompileBufferInProcedure( *pSubStaticMethod_System_TypeBase_InitializeUserTypes );
 | 
|---|
 | 829 | 
 | 
|---|
 | 830 |     if( IsNeedProcCompile() ){
 | 
|---|
 | 831 |         //プロシージャコンパイルによって、プロシージャコンパイルが必要になる場合
 | 
|---|
| [206] | 832 | 
 | 
|---|
 | 833 |         compiler.GetMeta().GetUserProcs().Iterator_Reset();
 | 
|---|
 | 834 |         while( compiler.GetMeta().GetUserProcs().Iterator_HasNext() )
 | 
|---|
 | 835 |         {
 | 
|---|
 | 836 |             UserProc *pUserProc = compiler.GetMeta().GetUserProcs().Iterator_GetNext();
 | 
|---|
 | 837 |             CompileBufferInProcedure( *pUserProc );
 | 
|---|
| [3] | 838 |         }
 | 
|---|
 | 839 |     }
 | 
|---|
 | 840 | 
 | 
|---|
 | 841 |     //_System_InitStaticLocalVariablesは一番最後にコンパイル
 | 
|---|
| [75] | 842 |     pSub_System_InitStaticLocalVariables->KillCompileStatus();
 | 
|---|
| [91] | 843 |     CompileBufferInProcedure( *pSub_System_InitStaticLocalVariables );
 | 
|---|
| [3] | 844 | 
 | 
|---|
 | 845 |     //_System_Call_Destructor_of_GlobalObjectは一番最後にコンパイル
 | 
|---|
| [75] | 846 |     pSub_System_Call_Destructor_of_GlobalObject->KillCompileStatus();
 | 
|---|
| [91] | 847 |     CompileBufferInProcedure( *pSub_System_Call_Destructor_of_GlobalObject );
 | 
|---|
| [3] | 848 | }
 | 
|---|