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