| 1 | #include "stdafx.h" | 
|---|
| 2 |  | 
|---|
| 3 | #include <jenga/include/smoothie/LexicalAnalysis.h> | 
|---|
| 4 |  | 
|---|
| 5 | #include <LexicalScope.h> | 
|---|
| 6 | #include <Compiler.h> | 
|---|
| 7 |  | 
|---|
| 8 | #include "../BasicCompiler_Common/common.h" | 
|---|
| 9 | #include "Opcode.h" | 
|---|
| 10 |  | 
|---|
| 11 | void OpcodeOthers( const char *Command ){ | 
|---|
| 12 | int i,i2; | 
|---|
| 13 |  | 
|---|
| 14 | char leftTerm[8192]; | 
|---|
| 15 | int lastParePos = 0; | 
|---|
| 16 | for(i=0;;i++){ | 
|---|
| 17 | if(Command[i]=='\"'){ | 
|---|
| 18 | //ダブルクォートは不正なのでエラー扱い | 
|---|
| 19 | leftTerm[i]=0; | 
|---|
| 20 | SetError(3,leftTerm,cp); | 
|---|
| 21 | return; | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 | if(Command[i]=='('){ | 
|---|
| 25 | lastParePos = i; | 
|---|
| 26 | i2=GetStringInPare(leftTerm+i,Command+i); | 
|---|
| 27 | i+=i2-1; | 
|---|
| 28 | continue; | 
|---|
| 29 | } | 
|---|
| 30 | if(Command[i]=='['){ | 
|---|
| 31 | i2=GetStringInBracket(leftTerm+i,Command+i); | 
|---|
| 32 | i+=i2-1; | 
|---|
| 33 | continue; | 
|---|
| 34 | } | 
|---|
| 35 | if(Command[i]=='\0'){ | 
|---|
| 36 | leftTerm[i] = 0; | 
|---|
| 37 | break; | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | if( IsNumCalcMark( Command, i ) ){ | 
|---|
| 41 | leftTerm[i] = 0; | 
|---|
| 42 | break; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | leftTerm[i]=Command[i]; | 
|---|
| 46 | } | 
|---|
| 47 | if(!( | 
|---|
| 48 | IsVariableTopChar(leftTerm[0])|| | 
|---|
| 49 | leftTerm[0]=='.'|| | 
|---|
| 50 | (leftTerm[0]==1&&leftTerm[1]==ESC_PSMEM) | 
|---|
| 51 | )){ | 
|---|
| 52 | SetError(1,NULL,cp); | 
|---|
| 53 | return; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 | if(Command[i]=='\0' && lastParePos == 0){ | 
|---|
| 58 | ////////////////////////////// | 
|---|
| 59 | // パラメータ無しのマクロ検索 | 
|---|
| 60 | ////////////////////////////// | 
|---|
| 61 |  | 
|---|
| 62 | const UserProc *pUserProc = GetSubHash(Command); | 
|---|
| 63 |  | 
|---|
| 64 | //GetSubHash内でエラー提示が行われた場合 | 
|---|
| 65 | if(pUserProc==(UserProc *)-1) return; | 
|---|
| 66 |  | 
|---|
| 67 | if(pUserProc==0){ | 
|---|
| 68 | char temporary[VN_SIZE]; | 
|---|
| 69 | lstrcpy(temporary,Command); | 
|---|
| 70 |  | 
|---|
| 71 | CharUpper(temporary); | 
|---|
| 72 | pUserProc=GetSubHash(temporary); | 
|---|
| 73 |  | 
|---|
| 74 | //GetSubHash内でエラー提示が行われた場合 | 
|---|
| 75 | if(pUserProc==(UserProc *)-1) return; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | if(pUserProc){ | 
|---|
| 79 | if( !pUserProc->IsMacro() ){ | 
|---|
| 80 | SetError(10,Command,cp); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | Opcode_CallProc("",pUserProc,0,"",0); | 
|---|
| 84 |  | 
|---|
| 85 | return; | 
|---|
| 86 | } | 
|---|
| 87 | } | 
|---|
| 88 | else if(IsNumCalcMark(Command,i)){ | 
|---|
| 89 | //代入演算 | 
|---|
| 90 | OpcodeCalc(Command); | 
|---|
| 91 | return; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 | Type resultType; | 
|---|
| 96 | bool isLiteral; | 
|---|
| 97 | BOOL bUseHeap; | 
|---|
| 98 | bool result = TermOpe( leftTerm, Type(), resultType, isLiteral, &bUseHeap, false, NULL, true ); | 
|---|
| 99 | if( result ){ | 
|---|
| 100 |  | 
|---|
| 101 | ///////////////////// | 
|---|
| 102 | // 戻り値の処理 | 
|---|
| 103 | ///////////////////// | 
|---|
| 104 |  | 
|---|
| 105 | if( resultType.IsReal() ){ | 
|---|
| 106 | //fstp st(0) | 
|---|
| 107 | compiler.codeGenerator.PutOld( | 
|---|
| 108 | (char)0xDD, | 
|---|
| 109 | (char)0xD8 | 
|---|
| 110 | ); | 
|---|
| 111 | } | 
|---|
| 112 | else if( resultType.IsStruct() ){ | 
|---|
| 113 | //mov ebx,eax | 
|---|
| 114 | compiler.codeGenerator.op_mov_RR(REG_EBX,REG_EAX); | 
|---|
| 115 |  | 
|---|
| 116 | FreeTempObject(REG_EBX,&resultType.GetClass()); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | //成功 | 
|---|
| 120 | return; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | // 失敗 | 
|---|
| 124 | SetError(1, NULL,cp); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void OpcodeIf(char *Parameter){ | 
|---|
| 128 | int i,i2; | 
|---|
| 129 | Type tempType; | 
|---|
| 130 |  | 
|---|
| 131 | for(i=0;;i++){ | 
|---|
| 132 | if(Parameter[i]=='\0'){ | 
|---|
| 133 | SetError(21,NULL,cp); | 
|---|
| 134 | return; | 
|---|
| 135 | } | 
|---|
| 136 | if(Parameter[i]==1&&Parameter[i+1]==ESC_THEN){ | 
|---|
| 137 | Parameter[i]=0; | 
|---|
| 138 | break; | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | const PertialSchedule *pIfPertialSchedule = NULL; | 
|---|
| 143 | if( !NumOpe(Parameter,Type(),tempType) ){ | 
|---|
| 144 | //NumOpe内でエラー | 
|---|
| 145 | } | 
|---|
| 146 | else if( tempType.IsDouble() ){ | 
|---|
| 147 | //fld qword ptr[esp] | 
|---|
| 148 | compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE); | 
|---|
| 149 |  | 
|---|
| 150 | //push 0 | 
|---|
| 151 | compiler.codeGenerator.op_push_V(0); | 
|---|
| 152 |  | 
|---|
| 153 | //fild dword ptr[esp] | 
|---|
| 154 | compiler.codeGenerator.op_fld_ptr_esp(DEF_LONG); | 
|---|
| 155 |  | 
|---|
| 156 | //add esp,sizeof(double)+sizeof(long) | 
|---|
| 157 | compiler.codeGenerator.op_add_esp(sizeof(double)+sizeof(long)); | 
|---|
| 158 |  | 
|---|
| 159 | //fcompp | 
|---|
| 160 | compiler.codeGenerator.op_fcompp(); | 
|---|
| 161 |  | 
|---|
| 162 | //fnstsw ax | 
|---|
| 163 | compiler.codeGenerator.op_fnstsw_ax(); | 
|---|
| 164 |  | 
|---|
| 165 | //test ah,40 | 
|---|
| 166 | compiler.codeGenerator.op_test_ah( (char)0x40 ); | 
|---|
| 167 |  | 
|---|
| 168 | //jne (endif、または else まで) | 
|---|
| 169 | pIfPertialSchedule = compiler.codeGenerator.op_jne( 0, sizeof(long), true ); | 
|---|
| 170 | } | 
|---|
| 171 | else if( tempType.IsSingle() ){ | 
|---|
| 172 | //fld dword ptr[esp] | 
|---|
| 173 | compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE); | 
|---|
| 174 |  | 
|---|
| 175 | //push 0 | 
|---|
| 176 | compiler.codeGenerator.op_push_V(0); | 
|---|
| 177 |  | 
|---|
| 178 | //fild dword ptr[esp] | 
|---|
| 179 | compiler.codeGenerator.op_fld_ptr_esp(DEF_LONG); | 
|---|
| 180 |  | 
|---|
| 181 | //add esp,sizeof(float)+sizeof(long) | 
|---|
| 182 | compiler.codeGenerator.op_add_esp(sizeof(float)+sizeof(long)); | 
|---|
| 183 |  | 
|---|
| 184 | //fcompp | 
|---|
| 185 | compiler.codeGenerator.op_fcompp(); | 
|---|
| 186 |  | 
|---|
| 187 | //fnstsw ax | 
|---|
| 188 | compiler.codeGenerator.op_fnstsw_ax(); | 
|---|
| 189 |  | 
|---|
| 190 | //test ah,40 | 
|---|
| 191 | compiler.codeGenerator.op_test_ah( (char)0x40 ); | 
|---|
| 192 |  | 
|---|
| 193 | //jne (endif、または else まで) | 
|---|
| 194 | pIfPertialSchedule = compiler.codeGenerator.op_jne( 0, sizeof(long), true ); | 
|---|
| 195 | } | 
|---|
| 196 | else if( tempType.Is64() ){ | 
|---|
| 197 | //64ビット型 | 
|---|
| 198 |  | 
|---|
| 199 | //pop eax | 
|---|
| 200 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 201 |  | 
|---|
| 202 | //pop ebx | 
|---|
| 203 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 204 |  | 
|---|
| 205 | //cmp eax,0 | 
|---|
| 206 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EAX, 0 ); | 
|---|
| 207 |  | 
|---|
| 208 | //jne | 
|---|
| 209 | const PertialSchedule *pTempPertialSchedule1 = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 210 |  | 
|---|
| 211 | //cmp ebx,0 | 
|---|
| 212 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EBX, 0 ); | 
|---|
| 213 |  | 
|---|
| 214 | //jne | 
|---|
| 215 | const PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 216 |  | 
|---|
| 217 | //jmp (endif、または else までジャンプ) | 
|---|
| 218 | pIfPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); | 
|---|
| 219 |  | 
|---|
| 220 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule1 ); | 
|---|
| 221 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule2 ); | 
|---|
| 222 | } | 
|---|
| 223 | else{ | 
|---|
| 224 | //32ビット型 | 
|---|
| 225 |  | 
|---|
| 226 | //pop eax | 
|---|
| 227 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 228 |  | 
|---|
| 229 | //cmp eax,0 | 
|---|
| 230 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EAX, 0 ); | 
|---|
| 231 |  | 
|---|
| 232 | //je (endif、または else まで条件ジャンプ) | 
|---|
| 233 | pIfPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true ); | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 |  | 
|---|
| 237 | ///////////////////////// | 
|---|
| 238 | // If内をコード化 | 
|---|
| 239 | ///////////////////////// | 
|---|
| 240 |  | 
|---|
| 241 | //レキシカルスコープをレベルアップ | 
|---|
| 242 | compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_IF ); | 
|---|
| 243 |  | 
|---|
| 244 | i2=CompileBuffer(ESC_ENDIF,0); | 
|---|
| 245 |  | 
|---|
| 246 | //レキシカルスコープをレベルダウン | 
|---|
| 247 | compiler.codeGenerator.lexicalScopes.End(); | 
|---|
| 248 |  | 
|---|
| 249 |  | 
|---|
| 250 | if( pIfPertialSchedule == NULL ) return; | 
|---|
| 251 |  | 
|---|
| 252 | if(i2==ESC_ELSE){ | 
|---|
| 253 | //jmp (endifまで) | 
|---|
| 254 | const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); | 
|---|
| 255 |  | 
|---|
| 256 | compiler.codeGenerator.opfix_JmpPertialSchedule( pIfPertialSchedule ); | 
|---|
| 257 |  | 
|---|
| 258 |  | 
|---|
| 259 | ///////////////////////// | 
|---|
| 260 | // Else内をコード化 | 
|---|
| 261 | ///////////////////////// | 
|---|
| 262 |  | 
|---|
| 263 | //レキシカルスコープをレベルアップ | 
|---|
| 264 | compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_IF ); | 
|---|
| 265 |  | 
|---|
| 266 | CompileBuffer(ESC_ENDIF,0); | 
|---|
| 267 |  | 
|---|
| 268 | //レキシカルスコープをレベルダウン | 
|---|
| 269 | compiler.codeGenerator.lexicalScopes.End(); | 
|---|
| 270 |  | 
|---|
| 271 |  | 
|---|
| 272 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule ); | 
|---|
| 273 | } | 
|---|
| 274 | else{ | 
|---|
| 275 | compiler.codeGenerator.opfix_JmpPertialSchedule( pIfPertialSchedule ); | 
|---|
| 276 | } | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | int GetLabelAddress(char *LabelName,int LineNum){ | 
|---|
| 280 | extern int MaxLabelNum; | 
|---|
| 281 | extern LABEL *pLabelNames; | 
|---|
| 282 | int i; | 
|---|
| 283 |  | 
|---|
| 284 | if(LabelName){ | 
|---|
| 285 | for(i=0;i<MaxLabelNum;i++){ | 
|---|
| 286 | if(pLabelNames[i].pName){ | 
|---|
| 287 | if(lstrcmp(LabelName,pLabelNames[i].pName)==0) return pLabelNames[i].address; | 
|---|
| 288 | } | 
|---|
| 289 | } | 
|---|
| 290 | } | 
|---|
| 291 | else{ | 
|---|
| 292 | for(i=0;i<MaxLabelNum;i++){ | 
|---|
| 293 | if(pLabelNames[i].pName==0){ | 
|---|
| 294 | if(LineNum==pLabelNames[i].line) return pLabelNames[i].address; | 
|---|
| 295 | } | 
|---|
| 296 | } | 
|---|
| 297 | } | 
|---|
| 298 | return -1; | 
|---|
| 299 | } | 
|---|
| 300 | void OpcodeGoto(char *Parameter){ | 
|---|
| 301 | extern HANDLE hHeap; | 
|---|
| 302 | int i,LineNum; | 
|---|
| 303 |  | 
|---|
| 304 | if(Parameter[0]=='*'){ | 
|---|
| 305 | i=GetLabelAddress(Parameter+1,0); | 
|---|
| 306 |  | 
|---|
| 307 | if( i == -1 ) | 
|---|
| 308 | { | 
|---|
| 309 | //jmp ...(schedule) | 
|---|
| 310 | compiler.codeGenerator.op_jmp_goto_schedule( GotoLabelSchedule( (const std::string)(Parameter + 1), obp, cp ) ); | 
|---|
| 311 | } | 
|---|
| 312 | else | 
|---|
| 313 | { | 
|---|
| 314 | //jmp ... | 
|---|
| 315 | compiler.codeGenerator.op_jmp( i-obp, sizeof(long), false, true ); | 
|---|
| 316 | } | 
|---|
| 317 | } | 
|---|
| 318 | else{ | 
|---|
| 319 | LineNum=atoi(Parameter); | 
|---|
| 320 | i=GetLabelAddress(0,LineNum); | 
|---|
| 321 |  | 
|---|
| 322 | if( i == -1 ) | 
|---|
| 323 | { | 
|---|
| 324 | //jmp ...(schedule) | 
|---|
| 325 | compiler.codeGenerator.op_jmp_goto_schedule( GotoLabelSchedule( LineNum, obp, cp ) ); | 
|---|
| 326 | } | 
|---|
| 327 | else | 
|---|
| 328 | { | 
|---|
| 329 | //jmp ... | 
|---|
| 330 | compiler.codeGenerator.op_jmp( i-obp, sizeof(long), false, true ); | 
|---|
| 331 | } | 
|---|
| 332 | } | 
|---|
| 333 | } | 
|---|
| 334 | void OpcodeWhile(char *Parameter){ | 
|---|
| 335 | extern HANDLE hHeap; | 
|---|
| 336 |  | 
|---|
| 337 | //Continueアドレスのバックアップとセット | 
|---|
| 338 | compiler.codeGenerator.ContinueAreaBegin(); | 
|---|
| 339 |  | 
|---|
| 340 | if(!Parameter[0]) SetError(10,"While",cp); | 
|---|
| 341 |  | 
|---|
| 342 | const PertialSchedule *pWhilePertialSchedule = NULL; | 
|---|
| 343 | Type tempType; | 
|---|
| 344 | if( !NumOpe(Parameter,Type(),tempType) ){ | 
|---|
| 345 | //ダミー | 
|---|
| 346 | } | 
|---|
| 347 | else if( tempType.IsDouble() ){ | 
|---|
| 348 | //fld qword ptr[esp] | 
|---|
| 349 | compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE); | 
|---|
| 350 |  | 
|---|
| 351 | //push 0 | 
|---|
| 352 | compiler.codeGenerator.op_push_V(0); | 
|---|
| 353 |  | 
|---|
| 354 | //fild dword ptr[esp] | 
|---|
| 355 | compiler.codeGenerator.op_fld_ptr_esp(DEF_LONG); | 
|---|
| 356 |  | 
|---|
| 357 | //add esp,sizeof(double)+sizeof(long) | 
|---|
| 358 | compiler.codeGenerator.op_add_esp(sizeof(double)+sizeof(long)); | 
|---|
| 359 |  | 
|---|
| 360 | //fcompp | 
|---|
| 361 | compiler.codeGenerator.op_fcompp(); | 
|---|
| 362 |  | 
|---|
| 363 | //fnstsw ax | 
|---|
| 364 | compiler.codeGenerator.op_fnstsw_ax(); | 
|---|
| 365 |  | 
|---|
| 366 | //test ah,40 | 
|---|
| 367 | compiler.codeGenerator.op_test_ah( (char)0x40 ); | 
|---|
| 368 |  | 
|---|
| 369 | //jne (Wend まで) | 
|---|
| 370 | pWhilePertialSchedule = compiler.codeGenerator.op_jne( 0, sizeof(long), true ); | 
|---|
| 371 | } | 
|---|
| 372 | else if( tempType.IsSingle() ){ | 
|---|
| 373 | //fld dword ptr[esp] | 
|---|
| 374 | compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE); | 
|---|
| 375 |  | 
|---|
| 376 | //push 0 | 
|---|
| 377 | compiler.codeGenerator.op_push_V(0); | 
|---|
| 378 |  | 
|---|
| 379 | //fild dword ptr[esp] | 
|---|
| 380 | compiler.codeGenerator.op_fld_ptr_esp(DEF_LONG); | 
|---|
| 381 |  | 
|---|
| 382 | //add esp,sizeof(float)+sizeof(long) | 
|---|
| 383 | compiler.codeGenerator.op_add_esp(sizeof(float)+sizeof(long)); | 
|---|
| 384 |  | 
|---|
| 385 | //fcompp | 
|---|
| 386 | compiler.codeGenerator.op_fcompp(); | 
|---|
| 387 |  | 
|---|
| 388 | //fnstsw ax | 
|---|
| 389 | compiler.codeGenerator.op_fnstsw_ax(); | 
|---|
| 390 |  | 
|---|
| 391 | //test ah,40h | 
|---|
| 392 | compiler.codeGenerator.op_test_ah( (char)0x40 ); | 
|---|
| 393 |  | 
|---|
| 394 | //jne (Wend まで) | 
|---|
| 395 | pWhilePertialSchedule = compiler.codeGenerator.op_jne( 0, sizeof(long), true ); | 
|---|
| 396 | } | 
|---|
| 397 | else if( tempType.Is64() ){ | 
|---|
| 398 | //64ビット型 | 
|---|
| 399 |  | 
|---|
| 400 | //pop eax | 
|---|
| 401 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 402 |  | 
|---|
| 403 | //pop ebx | 
|---|
| 404 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 405 |  | 
|---|
| 406 | //cmp eax,0 | 
|---|
| 407 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EAX, 0 ); | 
|---|
| 408 |  | 
|---|
| 409 | //jne | 
|---|
| 410 | const PertialSchedule *pTempPertialSchedule1 = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 411 |  | 
|---|
| 412 | //cmp ebx,0 | 
|---|
| 413 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EBX, 0 ); | 
|---|
| 414 |  | 
|---|
| 415 | //jne | 
|---|
| 416 | const PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 417 |  | 
|---|
| 418 | //jmp (Wendまでジャンプ) | 
|---|
| 419 | pWhilePertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); | 
|---|
| 420 |  | 
|---|
| 421 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule1 ); | 
|---|
| 422 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule2 ); | 
|---|
| 423 | } | 
|---|
| 424 | else{ | 
|---|
| 425 | //その他整数型 | 
|---|
| 426 |  | 
|---|
| 427 | //pop eax | 
|---|
| 428 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 429 |  | 
|---|
| 430 | //cmp eax,0 | 
|---|
| 431 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EAX, 0 ); | 
|---|
| 432 |  | 
|---|
| 433 | //je (Wend まで) | 
|---|
| 434 | pWhilePertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true ); | 
|---|
| 435 | } | 
|---|
| 436 |  | 
|---|
| 437 | //レキシカルスコープをレベルアップ | 
|---|
| 438 | compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_WHILE ); | 
|---|
| 439 |  | 
|---|
| 440 | //While内をコンパイル | 
|---|
| 441 | CompileBuffer(0,COM_WEND); | 
|---|
| 442 |  | 
|---|
| 443 | compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd(); | 
|---|
| 444 |  | 
|---|
| 445 | //jmp ... | 
|---|
| 446 | compiler.codeGenerator.op_jmp_continue(); | 
|---|
| 447 |  | 
|---|
| 448 | //レキシカルスコープをレベルダウン | 
|---|
| 449 | compiler.codeGenerator.lexicalScopes.End(); | 
|---|
| 450 |  | 
|---|
| 451 | if( pWhilePertialSchedule ) | 
|---|
| 452 | { | 
|---|
| 453 | compiler.codeGenerator.opfix_JmpPertialSchedule( pWhilePertialSchedule ); | 
|---|
| 454 | } | 
|---|
| 455 |  | 
|---|
| 456 | compiler.codeGenerator.ContinueAreaEnd(); | 
|---|
| 457 | } | 
|---|
| 458 |  | 
|---|
| 459 | char szNextVariable[VN_SIZE]; | 
|---|
| 460 | void OpcodeFor(char *Parameter){ | 
|---|
| 461 | extern HANDLE hHeap; | 
|---|
| 462 | int i,i2; | 
|---|
| 463 | char temporary[VN_SIZE],variable[VN_SIZE],JudgeNum[VN_SIZE],StepNum[VN_SIZE]; | 
|---|
| 464 |  | 
|---|
| 465 | //第1パラメータを取得 | 
|---|
| 466 | i=GetOneParameter(Parameter,0,temporary); | 
|---|
| 467 | if(!Parameter[i]){ | 
|---|
| 468 | SetError(12,"For",cp); | 
|---|
| 469 | goto ErrorStep; | 
|---|
| 470 | } | 
|---|
| 471 |  | 
|---|
| 472 | for(i2=0;;i2++){ | 
|---|
| 473 | if(temporary[i2]=='='){ | 
|---|
| 474 | variable[i2]=0; | 
|---|
| 475 |  | 
|---|
| 476 | //カウンタ初期化 | 
|---|
| 477 | OpcodeCalc(temporary); | 
|---|
| 478 | break; | 
|---|
| 479 | } | 
|---|
| 480 | if(temporary[i2]=='\0'){ | 
|---|
| 481 | SetError(12,"For",cp); | 
|---|
| 482 | goto ErrorStep; | 
|---|
| 483 | } | 
|---|
| 484 | variable[i2]=temporary[i2]; | 
|---|
| 485 | } | 
|---|
| 486 |  | 
|---|
| 487 | //jmp ... | 
|---|
| 488 | const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); | 
|---|
| 489 |  | 
|---|
| 490 | //Continueアドレスのバックアップとセット | 
|---|
| 491 | compiler.codeGenerator.ContinueAreaBegin(); | 
|---|
| 492 |  | 
|---|
| 493 | //第2パラメータを取得(to~) | 
|---|
| 494 | i=GetOneParameter(Parameter,i,JudgeNum); | 
|---|
| 495 |  | 
|---|
| 496 | //第3パラメータを取得(step~) | 
|---|
| 497 | if(Parameter[i]){ | 
|---|
| 498 | i=GetOneParameter(Parameter,i,StepNum); | 
|---|
| 499 | if(Parameter[i]) SetError(12,"For",cp); | 
|---|
| 500 | } | 
|---|
| 501 | else lstrcpy(StepNum,"1"); | 
|---|
| 502 |  | 
|---|
| 503 | //カウンタを増加させる | 
|---|
| 504 | sprintf(temporary,"%s=(%s)+(%s)",variable,variable,StepNum); | 
|---|
| 505 | OpcodeCalc(temporary); | 
|---|
| 506 |  | 
|---|
| 507 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule ); | 
|---|
| 508 |  | 
|---|
| 509 | //増加か減少かを区別する | 
|---|
| 510 | sprintf(temporary,"(%s)>=0",StepNum); | 
|---|
| 511 | NumOpe(temporary,Type(),Type()); | 
|---|
| 512 |  | 
|---|
| 513 | //pop eax | 
|---|
| 514 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 515 |  | 
|---|
| 516 | //cmp eax,0 | 
|---|
| 517 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EAX, 0 ); | 
|---|
| 518 |  | 
|---|
| 519 | //je [カウンタ減少の場合の判定] | 
|---|
| 520 | pTempPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true ); | 
|---|
| 521 |  | 
|---|
| 522 | //判定(カウンタ増加の場合) | 
|---|
| 523 | sprintf(temporary,"%s<=(%s)",variable,JudgeNum); | 
|---|
| 524 | NumOpe(temporary,Type(),Type()); | 
|---|
| 525 |  | 
|---|
| 526 | //pop eax | 
|---|
| 527 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 528 |  | 
|---|
| 529 | //jmp [カウンタ減少の場合の判定を飛び越す] | 
|---|
| 530 | const PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); | 
|---|
| 531 |  | 
|---|
| 532 | //jeジャンプ先のオフセット値 | 
|---|
| 533 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule ); | 
|---|
| 534 |  | 
|---|
| 535 | //判定(カウンタ減少の場合) | 
|---|
| 536 | sprintf(temporary,"%s>=(%s)",variable,JudgeNum); | 
|---|
| 537 | NumOpe(temporary,Type(),Type()); | 
|---|
| 538 |  | 
|---|
| 539 | //pop eax | 
|---|
| 540 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 541 |  | 
|---|
| 542 | //jmpジャンプ先のオフセット値 | 
|---|
| 543 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule2 ); | 
|---|
| 544 |  | 
|---|
| 545 | //cmp eax,0 | 
|---|
| 546 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EAX, 0 ); | 
|---|
| 547 |  | 
|---|
| 548 | ErrorStep: | 
|---|
| 549 |  | 
|---|
| 550 | //je ... | 
|---|
| 551 | pTempPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true ); | 
|---|
| 552 |  | 
|---|
| 553 | //レキシカルスコープをレベルアップ | 
|---|
| 554 | compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_FOR ); | 
|---|
| 555 |  | 
|---|
| 556 | //For内をコンパイル | 
|---|
| 557 | CompileBuffer(0,COM_NEXT); | 
|---|
| 558 |  | 
|---|
| 559 | compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd(); | 
|---|
| 560 |  | 
|---|
| 561 | if(szNextVariable[0]){ | 
|---|
| 562 | if(lstrcmp(szNextVariable,variable)!=0){ | 
|---|
| 563 | SetError(55,szNextVariable,cp); | 
|---|
| 564 | } | 
|---|
| 565 | } | 
|---|
| 566 |  | 
|---|
| 567 | //jmp ... | 
|---|
| 568 | compiler.codeGenerator.op_jmp_continue(); | 
|---|
| 569 |  | 
|---|
| 570 | //レキシカルスコープをレベルダウン | 
|---|
| 571 | compiler.codeGenerator.lexicalScopes.End(); | 
|---|
| 572 |  | 
|---|
| 573 | //jeジャンプ先のオフセット値 | 
|---|
| 574 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule ); | 
|---|
| 575 |  | 
|---|
| 576 | //Continueアドレスを復元 | 
|---|
| 577 | compiler.codeGenerator.ContinueAreaEnd(); | 
|---|
| 578 | } | 
|---|
| 579 |  | 
|---|
| 580 | void OpcodeDo(char *Parameter){ | 
|---|
| 581 | extern HANDLE hHeap; | 
|---|
| 582 | int i,i2,i3; | 
|---|
| 583 |  | 
|---|
| 584 | if(Parameter[0]) SetError(10,"Do",cp); | 
|---|
| 585 |  | 
|---|
| 586 | //Continueアドレスのバックアップとセット | 
|---|
| 587 | compiler.codeGenerator.ContinueAreaBegin(); | 
|---|
| 588 |  | 
|---|
| 589 | //レキシカルスコープをレベルアップ | 
|---|
| 590 | compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_DO ); | 
|---|
| 591 |  | 
|---|
| 592 | //Do内をコンパイル | 
|---|
| 593 | CompileBuffer(0,COM_LOOP); | 
|---|
| 594 |  | 
|---|
| 595 | compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd(); | 
|---|
| 596 |  | 
|---|
| 597 | const PertialSchedule *pDoPertialSchedule = NULL; | 
|---|
| 598 |  | 
|---|
| 599 | extern char *basbuf; | 
|---|
| 600 | char temporary[VN_SIZE]; | 
|---|
| 601 | for(i=cp-1;;i--){ | 
|---|
| 602 | if(IsCommandDelimitation(basbuf[i])){ | 
|---|
| 603 | i+=3; | 
|---|
| 604 | if(!(basbuf[i]=='0'||basbuf[i]=='1')){ | 
|---|
| 605 | //無条件ループ | 
|---|
| 606 | break; | 
|---|
| 607 | } | 
|---|
| 608 | i3=i; | 
|---|
| 609 |  | 
|---|
| 610 | for(i+=2,i2=0;;i++,i2++){ | 
|---|
| 611 | if(IsCommandDelimitation(basbuf[i])){ | 
|---|
| 612 | temporary[i2]=0; | 
|---|
| 613 | break; | 
|---|
| 614 | } | 
|---|
| 615 | temporary[i2]=basbuf[i]; | 
|---|
| 616 | } | 
|---|
| 617 |  | 
|---|
| 618 | Type tempType; | 
|---|
| 619 | NumOpe(temporary,Type(),tempType); | 
|---|
| 620 |  | 
|---|
| 621 | if( tempType.IsDouble() ){ | 
|---|
| 622 | //fld qword ptr[esp] | 
|---|
| 623 | compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE); | 
|---|
| 624 |  | 
|---|
| 625 | //push 0 | 
|---|
| 626 | compiler.codeGenerator.op_push_V(0); | 
|---|
| 627 |  | 
|---|
| 628 | //fild dword ptr[esp] | 
|---|
| 629 | compiler.codeGenerator.op_fld_ptr_esp(DEF_LONG); | 
|---|
| 630 |  | 
|---|
| 631 | //add esp,sizeof(double)+sizeof(long) | 
|---|
| 632 | compiler.codeGenerator.op_add_esp(sizeof(double)+sizeof(long)); | 
|---|
| 633 |  | 
|---|
| 634 | //fcompp | 
|---|
| 635 | compiler.codeGenerator.op_fcompp(); | 
|---|
| 636 |  | 
|---|
| 637 | //fnstsw ax | 
|---|
| 638 | compiler.codeGenerator.op_fnstsw_ax(); | 
|---|
| 639 |  | 
|---|
| 640 | //test ah,40 | 
|---|
| 641 | compiler.codeGenerator.op_test_ah( (char)0x40 ); | 
|---|
| 642 |  | 
|---|
| 643 | if(basbuf[i3]=='0'){ | 
|---|
| 644 | //While | 
|---|
| 645 |  | 
|---|
| 646 | //jne 5(ループ終了) | 
|---|
| 647 | pDoPertialSchedule = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 648 | } | 
|---|
| 649 | else if(basbuf[i3]=='1'){ | 
|---|
| 650 | //Until | 
|---|
| 651 |  | 
|---|
| 652 | //je 5(ループ終了) | 
|---|
| 653 | pDoPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(char), true ); | 
|---|
| 654 | } | 
|---|
| 655 | } | 
|---|
| 656 | else if( tempType.IsSingle() ){ | 
|---|
| 657 | //fld dword ptr[esp] | 
|---|
| 658 | compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE); | 
|---|
| 659 |  | 
|---|
| 660 | //push 0 | 
|---|
| 661 | compiler.codeGenerator.op_push_V(0); | 
|---|
| 662 |  | 
|---|
| 663 | //fild dword ptr[esp] | 
|---|
| 664 | compiler.codeGenerator.op_fld_ptr_esp(DEF_LONG); | 
|---|
| 665 |  | 
|---|
| 666 | //add esp,sizeof(float)+sizeof(long) | 
|---|
| 667 | compiler.codeGenerator.op_add_esp(sizeof(float)+sizeof(long)); | 
|---|
| 668 |  | 
|---|
| 669 | //fcompp | 
|---|
| 670 | compiler.codeGenerator.op_fcompp(); | 
|---|
| 671 |  | 
|---|
| 672 | //fnstsw ax | 
|---|
| 673 | compiler.codeGenerator.op_fnstsw_ax(); | 
|---|
| 674 |  | 
|---|
| 675 | //test ah,40 | 
|---|
| 676 | compiler.codeGenerator.op_test_ah( (char)0x40 ); | 
|---|
| 677 |  | 
|---|
| 678 | if(basbuf[i3]=='0'){ | 
|---|
| 679 | //While | 
|---|
| 680 |  | 
|---|
| 681 | //jne 5(ループ終了) | 
|---|
| 682 | pDoPertialSchedule = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 683 | } | 
|---|
| 684 | else if(basbuf[i3]=='1'){ | 
|---|
| 685 | //Until | 
|---|
| 686 |  | 
|---|
| 687 | //je 5(ループ終了) | 
|---|
| 688 | pDoPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(char), true ); | 
|---|
| 689 | } | 
|---|
| 690 | } | 
|---|
| 691 | else if( tempType.Is64() ){ | 
|---|
| 692 | //64ビット型 | 
|---|
| 693 |  | 
|---|
| 694 | //pop eax | 
|---|
| 695 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 696 |  | 
|---|
| 697 | //pop ebx | 
|---|
| 698 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 699 |  | 
|---|
| 700 | //cmp eax,0 | 
|---|
| 701 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EAX, 0 ); | 
|---|
| 702 |  | 
|---|
| 703 | //jne | 
|---|
| 704 | const PertialSchedule *pTempPertialSchedule1 = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 705 |  | 
|---|
| 706 | //cmp ebx,0 | 
|---|
| 707 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EBX, 0 ); | 
|---|
| 708 |  | 
|---|
| 709 | //jne | 
|---|
| 710 | const PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 711 |  | 
|---|
| 712 | if(basbuf[i3]=='0'){ | 
|---|
| 713 | //While | 
|---|
| 714 |  | 
|---|
| 715 | //jmp 5(ループ終了) | 
|---|
| 716 | pDoPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(char), true ); | 
|---|
| 717 |  | 
|---|
| 718 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule1 ); | 
|---|
| 719 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule2 ); | 
|---|
| 720 | } | 
|---|
| 721 | else if(basbuf[i3]=='1'){ | 
|---|
| 722 | //Until | 
|---|
| 723 |  | 
|---|
| 724 | //jmp 2(ループを続ける) | 
|---|
| 725 | const PertialSchedule *pTempPertialSchedule3 = compiler.codeGenerator.op_jmp( 0, sizeof(char), true ); | 
|---|
| 726 |  | 
|---|
| 727 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule1 ); | 
|---|
| 728 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule2 ); | 
|---|
| 729 |  | 
|---|
| 730 | //jmp 5(ループ終了) | 
|---|
| 731 | pDoPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(char), true ); | 
|---|
| 732 |  | 
|---|
| 733 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule3 ); | 
|---|
| 734 | } | 
|---|
| 735 | } | 
|---|
| 736 | else{ | 
|---|
| 737 | //pop eax | 
|---|
| 738 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 739 |  | 
|---|
| 740 | //cmp eax,0 | 
|---|
| 741 | compiler.codeGenerator.op_cmp_value( sizeof(long), REG_EAX, 0 ); | 
|---|
| 742 |  | 
|---|
| 743 | if(basbuf[i3]=='0'){ | 
|---|
| 744 | //While | 
|---|
| 745 |  | 
|---|
| 746 | //je 5(ループ終了) | 
|---|
| 747 | pDoPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(char), true ); | 
|---|
| 748 | } | 
|---|
| 749 | else if(basbuf[i3]=='1'){ | 
|---|
| 750 | //Until | 
|---|
| 751 |  | 
|---|
| 752 | //jne 5(ループ終了) | 
|---|
| 753 | pDoPertialSchedule = compiler.codeGenerator.op_jne( 0, sizeof(char), true ); | 
|---|
| 754 | } | 
|---|
| 755 | } | 
|---|
| 756 | break; | 
|---|
| 757 | } | 
|---|
| 758 | } | 
|---|
| 759 |  | 
|---|
| 760 | //jmp ... | 
|---|
| 761 | compiler.codeGenerator.op_jmp_continue(); | 
|---|
| 762 |  | 
|---|
| 763 | if( pDoPertialSchedule ) | 
|---|
| 764 | { | 
|---|
| 765 | compiler.codeGenerator.opfix_JmpPertialSchedule( pDoPertialSchedule ); | 
|---|
| 766 | } | 
|---|
| 767 |  | 
|---|
| 768 | //jmp ... | 
|---|
| 769 | const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true ); | 
|---|
| 770 |  | 
|---|
| 771 | //レキシカルスコープをレベルダウン | 
|---|
| 772 | compiler.codeGenerator.lexicalScopes.End(); | 
|---|
| 773 |  | 
|---|
| 774 | //jmpジャンプ先のオフセット値 | 
|---|
| 775 | compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule ); | 
|---|
| 776 |  | 
|---|
| 777 | //Continueアドレスを復元 | 
|---|
| 778 | compiler.codeGenerator.ContinueAreaEnd(); | 
|---|
| 779 | } | 
|---|
| 780 | void OpcodeContinue(void){ | 
|---|
| 781 | //jmp ...(Continue addr) | 
|---|
| 782 | compiler.codeGenerator.op_jmp_continue(); | 
|---|
| 783 | } | 
|---|
| 784 |  | 
|---|
| 785 | void OpcodeExitSub(void){ | 
|---|
| 786 | extern HANDLE hHeap; | 
|---|
| 787 |  | 
|---|
| 788 | if( UserProc::IsGlobalAreaCompiling() ){ | 
|---|
| 789 | SetError(12,"Exit Sub/Function",cp); | 
|---|
| 790 | return; | 
|---|
| 791 | } | 
|---|
| 792 |  | 
|---|
| 793 | //未解放のローカルオブジェクトのデストラクタを呼び出す | 
|---|
| 794 | compiler.codeGenerator.lexicalScopes.CallDestructorsOfReturn(); | 
|---|
| 795 |  | 
|---|
| 796 | //jmp ...(End Sub/Function) | 
|---|
| 797 | compiler.codeGenerator.op_jmp_exitsub(); | 
|---|
| 798 | } | 
|---|
| 799 |  | 
|---|
| 800 | //Caseスケジュール | 
|---|
| 801 | class SelectSchedule | 
|---|
| 802 | { | 
|---|
| 803 | public: | 
|---|
| 804 | SelectSchedule( int typeSize ) | 
|---|
| 805 | : typeSize( typeSize ) | 
|---|
| 806 | , nowCaseSchedule( 0 ) | 
|---|
| 807 | { | 
|---|
| 808 | } | 
|---|
| 809 |  | 
|---|
| 810 | PertialSchedules casePertialSchedules; | 
|---|
| 811 | int typeSize; | 
|---|
| 812 | int nowCaseSchedule; | 
|---|
| 813 | }; | 
|---|
| 814 | std::vector<SelectSchedule> selectSchedules; | 
|---|
| 815 |  | 
|---|
| 816 | void OpcodeSelect(const char *lpszParms){ | 
|---|
| 817 | extern HANDLE hHeap; | 
|---|
| 818 | extern char *basbuf; | 
|---|
| 819 | int i,i2,i3,sw,NowCaseCp; | 
|---|
| 820 | char temporary[VN_SIZE]; | 
|---|
| 821 |  | 
|---|
| 822 | Type type1; | 
|---|
| 823 | if( !NumOpe(lpszParms,Type(), type1 ) ){ | 
|---|
| 824 | return; | 
|---|
| 825 | } | 
|---|
| 826 |  | 
|---|
| 827 | selectSchedules.push_back( SelectSchedule( type1.GetSize() ) ); | 
|---|
| 828 |  | 
|---|
| 829 | if( selectSchedules.back().typeSize < sizeof(long) ){ | 
|---|
| 830 | selectSchedules.back().typeSize = sizeof(long); | 
|---|
| 831 | } | 
|---|
| 832 |  | 
|---|
| 833 | for(i=cp,sw=0;;i++){ | 
|---|
| 834 | if(basbuf[i]=='\0'){ | 
|---|
| 835 | selectSchedules.pop_back(); | 
|---|
| 836 | SetError(22,"Select",cp); | 
|---|
| 837 | return; | 
|---|
| 838 | } | 
|---|
| 839 | if(basbuf[i]==1&&basbuf[i+1]==ESC_SELECTCASE){ | 
|---|
| 840 | for(i2=0;;i++){ | 
|---|
| 841 | if(basbuf[i]==1&&basbuf[i+1]==ESC_SELECTCASE) i2++; | 
|---|
| 842 | if(basbuf[i]==1&&basbuf[i+1]==ESC_ENDSELECT){ | 
|---|
| 843 | i2--; | 
|---|
| 844 | if(i2==0) break; | 
|---|
| 845 | } | 
|---|
| 846 | } | 
|---|
| 847 | continue; | 
|---|
| 848 | } | 
|---|
| 849 | if(basbuf[i]==1&&basbuf[i+1]==ESC_ENDSELECT){ | 
|---|
| 850 | if(sw==0){ | 
|---|
| 851 | //add esp,CaseTypeSize | 
|---|
| 852 | compiler.codeGenerator.op_add_esp( selectSchedules.back().typeSize ); | 
|---|
| 853 | } | 
|---|
| 854 | break; | 
|---|
| 855 | } | 
|---|
| 856 | if(basbuf[i]==1&&basbuf[i+1]==ESC_CASE){ | 
|---|
| 857 | NowCaseCp=i; | 
|---|
| 858 |  | 
|---|
| 859 | i++; | 
|---|
| 860 | while(1){ | 
|---|
| 861 | for(i++,i2=0;;i++,i2++){ | 
|---|
| 862 | if(basbuf[i]=='\"'){ | 
|---|
| 863 | i3=GetStringInQuotation(temporary+i2,basbuf+i); | 
|---|
| 864 | i+=i3-1; | 
|---|
| 865 | i2+=i3-1; | 
|---|
| 866 | continue; | 
|---|
| 867 | } | 
|---|
| 868 | if(basbuf[i]=='('){ | 
|---|
| 869 | i3=GetStringInPare(temporary+i2,basbuf+i); | 
|---|
| 870 | i+=i3-1; | 
|---|
| 871 | i2+=i3-1; | 
|---|
| 872 | continue; | 
|---|
| 873 | } | 
|---|
| 874 | if(basbuf[i]=='['){ | 
|---|
| 875 | i3=GetStringInBracket(temporary+i2,basbuf+i); | 
|---|
| 876 | i+=i3-1; | 
|---|
| 877 | i2+=i3-1; | 
|---|
| 878 | continue; | 
|---|
| 879 | } | 
|---|
| 880 |  | 
|---|
| 881 | if(IsCommandDelimitation(basbuf[i])){ | 
|---|
| 882 | temporary[i2]=0; | 
|---|
| 883 | break; | 
|---|
| 884 | } | 
|---|
| 885 | if(basbuf[i]==','){ | 
|---|
| 886 | temporary[i2]=0; | 
|---|
| 887 | break; | 
|---|
| 888 | } | 
|---|
| 889 |  | 
|---|
| 890 | temporary[i2]=basbuf[i]; | 
|---|
| 891 | } | 
|---|
| 892 |  | 
|---|
| 893 | //エラー用 | 
|---|
| 894 | i2=cp; | 
|---|
| 895 | cp=NowCaseCp; | 
|---|
| 896 |  | 
|---|
| 897 | Type type2; | 
|---|
| 898 | if( !NumOpe(temporary,type1,type2) ){ | 
|---|
| 899 | return; | 
|---|
| 900 | } | 
|---|
| 901 |  | 
|---|
| 902 | cp=i2; | 
|---|
| 903 |  | 
|---|
| 904 | if(type1.IsObject()){ | 
|---|
| 905 | std::vector<const UserProc *> subs; | 
|---|
| 906 | type1.GetClass().GetMethods().Enum( CALC_EQUAL, subs ); | 
|---|
| 907 | if( subs.size() == 0 ){ | 
|---|
| 908 | return; | 
|---|
| 909 | } | 
|---|
| 910 |  | 
|---|
| 911 | Parameters params; | 
|---|
| 912 | params.push_back( new Parameter( "", Type( type2 ) ) ); | 
|---|
| 913 |  | 
|---|
| 914 | //オーバーロードを解決 | 
|---|
| 915 | const UserProc *pUserProc = OverloadSolution("==",subs, params, NULL); | 
|---|
| 916 |  | 
|---|
| 917 | delete params[0]; | 
|---|
| 918 |  | 
|---|
| 919 | if(!pUserProc){ | 
|---|
| 920 | //エラー | 
|---|
| 921 | return; | 
|---|
| 922 | } | 
|---|
| 923 |  | 
|---|
| 924 |  | 
|---|
| 925 | //pop edx | 
|---|
| 926 | compiler.codeGenerator.op_pop(REG_EDX); | 
|---|
| 927 |  | 
|---|
| 928 | //mov ecx,dword ptr[esp] | 
|---|
| 929 | compiler.codeGenerator.op_mov_RM(sizeof(long),REG_ECX,REG_ESP,0,MOD_BASE); | 
|---|
| 930 |  | 
|---|
| 931 | //push edx | 
|---|
| 932 | compiler.codeGenerator.op_push(REG_EDX); | 
|---|
| 933 |  | 
|---|
| 934 | //push ecx | 
|---|
| 935 | compiler.codeGenerator.op_push(REG_ECX); | 
|---|
| 936 |  | 
|---|
| 937 | //call operator_proc    ※ ==演算子 | 
|---|
| 938 | compiler.codeGenerator.op_call(pUserProc); | 
|---|
| 939 |  | 
|---|
| 940 | //test eax,eax | 
|---|
| 941 | compiler.codeGenerator.op_test(REG_EAX,REG_EAX); | 
|---|
| 942 |  | 
|---|
| 943 | //jne ... | 
|---|
| 944 | selectSchedules.back().casePertialSchedules.push_back( | 
|---|
| 945 | compiler.codeGenerator.op_jne( 0, sizeof(long), true ) | 
|---|
| 946 | ); | 
|---|
| 947 | } | 
|---|
| 948 | else if(type1.IsDouble()){ | 
|---|
| 949 | ChangeTypeToDouble(type2.GetBasicType()); | 
|---|
| 950 |  | 
|---|
| 951 | //fld qword ptr[esp] | 
|---|
| 952 | compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE); | 
|---|
| 953 |  | 
|---|
| 954 | //add esp,CaseTypeSize | 
|---|
| 955 | compiler.codeGenerator.op_add_esp(selectSchedules.back().typeSize); | 
|---|
| 956 |  | 
|---|
| 957 | //fld qword ptr[esp] | 
|---|
| 958 | compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE); | 
|---|
| 959 |  | 
|---|
| 960 | //fcompp | 
|---|
| 961 | compiler.codeGenerator.op_fcompp(); | 
|---|
| 962 |  | 
|---|
| 963 | //fnstsw ax | 
|---|
| 964 | compiler.codeGenerator.op_fnstsw_ax(); | 
|---|
| 965 |  | 
|---|
| 966 | //test ah,40 | 
|---|
| 967 | compiler.codeGenerator.op_test_ah( (char)0x40 ); | 
|---|
| 968 |  | 
|---|
| 969 | //jne ... | 
|---|
| 970 | selectSchedules.back().casePertialSchedules.push_back( | 
|---|
| 971 | compiler.codeGenerator.op_jne( 0, sizeof(long), true ) | 
|---|
| 972 | ); | 
|---|
| 973 | } | 
|---|
| 974 | else if(type1.IsSingle()){ | 
|---|
| 975 | ChangeTypeToSingle(type2.GetBasicType()); | 
|---|
| 976 |  | 
|---|
| 977 | //fld dword ptr[esp] | 
|---|
| 978 | compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE); | 
|---|
| 979 |  | 
|---|
| 980 | //add esp,CaseTypeSize | 
|---|
| 981 | compiler.codeGenerator.op_add_esp(selectSchedules.back().typeSize); | 
|---|
| 982 |  | 
|---|
| 983 | //fld dword ptr[esp] | 
|---|
| 984 | compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE); | 
|---|
| 985 |  | 
|---|
| 986 | //fcompp | 
|---|
| 987 | compiler.codeGenerator.op_fcompp(); | 
|---|
| 988 |  | 
|---|
| 989 | //fnstsw ax | 
|---|
| 990 | compiler.codeGenerator.op_fnstsw_ax(); | 
|---|
| 991 |  | 
|---|
| 992 | //test ah,40 | 
|---|
| 993 | compiler.codeGenerator.op_test_ah( (char)0x40 ); | 
|---|
| 994 |  | 
|---|
| 995 | //jne ... | 
|---|
| 996 | selectSchedules.back().casePertialSchedules.push_back( | 
|---|
| 997 | compiler.codeGenerator.op_jne( 0, sizeof(long), true ) | 
|---|
| 998 | ); | 
|---|
| 999 | } | 
|---|
| 1000 | else{ | 
|---|
| 1001 | //その他整数型 | 
|---|
| 1002 |  | 
|---|
| 1003 | //pop ebx | 
|---|
| 1004 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 1005 |  | 
|---|
| 1006 | //mov eax,dword ptr[esp] | 
|---|
| 1007 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_ESP, 0, MOD_BASE ); | 
|---|
| 1008 |  | 
|---|
| 1009 | //cmp eax,ebx | 
|---|
| 1010 | compiler.codeGenerator.op_cmp_RR( REG_EAX, REG_EBX ); | 
|---|
| 1011 |  | 
|---|
| 1012 | //je ... | 
|---|
| 1013 | selectSchedules.back().casePertialSchedules.push_back( | 
|---|
| 1014 | compiler.codeGenerator.op_je( 0, sizeof(long), true ) | 
|---|
| 1015 | ); | 
|---|
| 1016 | } | 
|---|
| 1017 |  | 
|---|
| 1018 | if(basbuf[i]!=',') break; | 
|---|
| 1019 | } | 
|---|
| 1020 | } | 
|---|
| 1021 | if(basbuf[i]==1&&basbuf[i+1]==ESC_CASEELSE){ | 
|---|
| 1022 | sw=1; | 
|---|
| 1023 |  | 
|---|
| 1024 | //jmp ... | 
|---|
| 1025 | selectSchedules.back().casePertialSchedules.push_back( | 
|---|
| 1026 | compiler.codeGenerator.op_jmp( 0, sizeof(long), true ) | 
|---|
| 1027 | ); | 
|---|
| 1028 | } | 
|---|
| 1029 | } | 
|---|
| 1030 |  | 
|---|
| 1031 | //レキシカルスコープをレベルアップ | 
|---|
| 1032 | compiler.codeGenerator.lexicalScopes.Start( obp, LexicalScope::SCOPE_TYPE_SELECT ); | 
|---|
| 1033 |  | 
|---|
| 1034 | //Select Case内をコンパイル | 
|---|
| 1035 | CompileBuffer(ESC_ENDSELECT,0); | 
|---|
| 1036 |  | 
|---|
| 1037 | //jmp EndSelect | 
|---|
| 1038 | selectSchedules.back().casePertialSchedules.push_back( | 
|---|
| 1039 | compiler.codeGenerator.op_jmp( 0, sizeof(long), true ) | 
|---|
| 1040 | ); | 
|---|
| 1041 |  | 
|---|
| 1042 | //最終スケジュール | 
|---|
| 1043 | for(i=selectSchedules.back().nowCaseSchedule;i<(int)selectSchedules.back().casePertialSchedules.size();i++){ | 
|---|
| 1044 | compiler.codeGenerator.opfix_JmpPertialSchedule( selectSchedules.back().casePertialSchedules[i] ); | 
|---|
| 1045 | } | 
|---|
| 1046 |  | 
|---|
| 1047 | //レキシカルスコープをレベルダウン | 
|---|
| 1048 | compiler.codeGenerator.lexicalScopes.End(); | 
|---|
| 1049 |  | 
|---|
| 1050 | selectSchedules.pop_back(); | 
|---|
| 1051 | } | 
|---|
| 1052 | void OpcodeCase(char *Parameter){ | 
|---|
| 1053 | int i; | 
|---|
| 1054 |  | 
|---|
| 1055 | if(selectSchedules.back().typeSize==-1){ | 
|---|
| 1056 | SetError(30,"Case",cp); | 
|---|
| 1057 | return; | 
|---|
| 1058 | } | 
|---|
| 1059 |  | 
|---|
| 1060 | //jmp EndSelect | 
|---|
| 1061 | selectSchedules.back().casePertialSchedules.push_back( | 
|---|
| 1062 | compiler.codeGenerator.op_jmp( 0, sizeof(long), true ) | 
|---|
| 1063 | ); | 
|---|
| 1064 |  | 
|---|
| 1065 | i=0; | 
|---|
| 1066 | while(1){ | 
|---|
| 1067 | //Caseスケジュール | 
|---|
| 1068 | compiler.codeGenerator.opfix_JmpPertialSchedule( selectSchedules.back().casePertialSchedules[selectSchedules.back().nowCaseSchedule] ); | 
|---|
| 1069 | selectSchedules.back().nowCaseSchedule++; | 
|---|
| 1070 |  | 
|---|
| 1071 | i=JumpOneParameter(Parameter,i); | 
|---|
| 1072 | if(Parameter[i]=='\0') break; | 
|---|
| 1073 | } | 
|---|
| 1074 |  | 
|---|
| 1075 | //add esp,CaseTypeSize | 
|---|
| 1076 | compiler.codeGenerator.op_add_esp(selectSchedules.back().typeSize); | 
|---|
| 1077 | } | 
|---|
| 1078 |  | 
|---|
| 1079 | void OpcodeGosub(char *Parameter){ | 
|---|
| 1080 | extern HANDLE hHeap; | 
|---|
| 1081 | int i,LineNum; | 
|---|
| 1082 |  | 
|---|
| 1083 | if(Parameter[0]=='*'){ | 
|---|
| 1084 | i=GetLabelAddress(Parameter+1,0); | 
|---|
| 1085 |  | 
|---|
| 1086 | if( i == -1 ) | 
|---|
| 1087 | { | 
|---|
| 1088 | //jmp ...(schedule) | 
|---|
| 1089 | compiler.codeGenerator.op_jmp_goto_schedule( GotoLabelSchedule( (const std::string)(Parameter + 1), obp, cp ) ); | 
|---|
| 1090 | } | 
|---|
| 1091 | else | 
|---|
| 1092 | { | 
|---|
| 1093 | //jmp ... | 
|---|
| 1094 | compiler.codeGenerator.op_jmp( i-obp, sizeof(long), false, true ); | 
|---|
| 1095 | } | 
|---|
| 1096 | } | 
|---|
| 1097 | else{ | 
|---|
| 1098 | LineNum=atoi(Parameter); | 
|---|
| 1099 | i=GetLabelAddress(0,LineNum); | 
|---|
| 1100 |  | 
|---|
| 1101 | if( i == -1 ) | 
|---|
| 1102 | { | 
|---|
| 1103 | //jmp ...(schedule) | 
|---|
| 1104 | compiler.codeGenerator.op_jmp_goto_schedule( GotoLabelSchedule( LineNum, obp, cp ) ); | 
|---|
| 1105 | } | 
|---|
| 1106 | else | 
|---|
| 1107 | { | 
|---|
| 1108 | //jmp ... | 
|---|
| 1109 | compiler.codeGenerator.op_jmp( i-obp, sizeof(long), false, true ); | 
|---|
| 1110 | } | 
|---|
| 1111 | } | 
|---|
| 1112 | } | 
|---|
| 1113 | void OpcodeReturn(char *Parameter){ | 
|---|
| 1114 | if( UserProc::IsGlobalAreaCompiling() ){ | 
|---|
| 1115 | //Gosub~Returnとして扱う | 
|---|
| 1116 |  | 
|---|
| 1117 | //ret | 
|---|
| 1118 | compiler.codeGenerator.op_ret(); | 
|---|
| 1119 | } | 
|---|
| 1120 | else{ | 
|---|
| 1121 | //戻り値をセット | 
|---|
| 1122 | if(Parameter[0]){ | 
|---|
| 1123 | const UserProc &proc = UserProc::CompilingUserProc(); | 
|---|
| 1124 |  | 
|---|
| 1125 | const char *temp = "_System_ReturnValue"; | 
|---|
| 1126 | if(proc.GetName()[0]==1&&proc.GetName()[1]==ESC_OPERATOR) | 
|---|
| 1127 | { | 
|---|
| 1128 | } | 
|---|
| 1129 | else{ | 
|---|
| 1130 | temp=proc.GetName().c_str(); | 
|---|
| 1131 | } | 
|---|
| 1132 |  | 
|---|
| 1133 | char temporary[VN_SIZE]; | 
|---|
| 1134 | sprintf(temporary,"%s=%s",temp,Parameter); | 
|---|
| 1135 | OpcodeCalc(temporary); | 
|---|
| 1136 | } | 
|---|
| 1137 |  | 
|---|
| 1138 | //プロシージャを抜け出す(C言語のreturnと同様の処理を行う) | 
|---|
| 1139 | OpcodeExitSub(); | 
|---|
| 1140 | } | 
|---|
| 1141 | } | 
|---|
| 1142 |  | 
|---|
| 1143 |  | 
|---|
| 1144 | //////////// | 
|---|
| 1145 | // ポインタ | 
|---|
| 1146 |  | 
|---|
| 1147 | void OpcodeSetPtrData(char *Parameter,int type){ | 
|---|
| 1148 | int i; | 
|---|
| 1149 | char temporary[VN_SIZE]; | 
|---|
| 1150 |  | 
|---|
| 1151 | if(Parameter[0]=='('){ | 
|---|
| 1152 | i=JumpStringInPare(Parameter,1); | 
|---|
| 1153 | if(Parameter[i+1]=='\0'){ | 
|---|
| 1154 | for(i=0;;i++){ | 
|---|
| 1155 | Parameter[i]=Parameter[i+1]; | 
|---|
| 1156 | if(Parameter[i]=='\0') break; | 
|---|
| 1157 | } | 
|---|
| 1158 | Parameter[i-1]=0; | 
|---|
| 1159 | } | 
|---|
| 1160 | } | 
|---|
| 1161 |  | 
|---|
| 1162 | //第1パラメータを取得 | 
|---|
| 1163 | i=GetOneParameter(Parameter,0,temporary); | 
|---|
| 1164 | if(!Parameter[i]){ | 
|---|
| 1165 | SetError(1,NULL,cp); | 
|---|
| 1166 | return; | 
|---|
| 1167 | } | 
|---|
| 1168 |  | 
|---|
| 1169 | Type resultType; | 
|---|
| 1170 | if( !NumOpe(temporary,Type(),resultType) ){ | 
|---|
| 1171 | return; | 
|---|
| 1172 | } | 
|---|
| 1173 | if(!resultType.IsWhole()){ | 
|---|
| 1174 | SetError(11,Parameter,cp); | 
|---|
| 1175 | return; | 
|---|
| 1176 | } | 
|---|
| 1177 |  | 
|---|
| 1178 | ChangeTypeToLong( resultType.GetBasicType() ); | 
|---|
| 1179 |  | 
|---|
| 1180 | //第2パラメータを取得 | 
|---|
| 1181 | i=GetOneParameter(Parameter,i,temporary); | 
|---|
| 1182 | if(Parameter[i]){ | 
|---|
| 1183 | SetError(1,NULL,cp); | 
|---|
| 1184 | return; | 
|---|
| 1185 | } | 
|---|
| 1186 |  | 
|---|
| 1187 | if( !NumOpe(temporary,Type(),resultType) ){ | 
|---|
| 1188 | return; | 
|---|
| 1189 | } | 
|---|
| 1190 |  | 
|---|
| 1191 | if(type==DEF_DOUBLE){ | 
|---|
| 1192 | ChangeTypeToDouble_ToFpuReg( resultType.GetBasicType() ); | 
|---|
| 1193 |  | 
|---|
| 1194 | //pop eax | 
|---|
| 1195 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 1196 |  | 
|---|
| 1197 | //fstp qword ptr[eax] | 
|---|
| 1198 | compiler.codeGenerator.PutOld( | 
|---|
| 1199 | (char)0xDD, | 
|---|
| 1200 | (char)0x18 | 
|---|
| 1201 | ); | 
|---|
| 1202 | } | 
|---|
| 1203 | else if(type==DEF_SINGLE){ | 
|---|
| 1204 | ChangeTypeToSingle( resultType.GetBasicType() ); | 
|---|
| 1205 |  | 
|---|
| 1206 | //pop ebx | 
|---|
| 1207 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 1208 |  | 
|---|
| 1209 | //pop eax | 
|---|
| 1210 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 1211 |  | 
|---|
| 1212 | //mov dword ptr[eax],ebx | 
|---|
| 1213 | compiler.codeGenerator.op_mov_MR( sizeof(long), REG_EBX, REG_EAX, 0, MOD_BASE ); | 
|---|
| 1214 | } | 
|---|
| 1215 | else if(type==DEF_QWORD){ | 
|---|
| 1216 | ChangeTypeToInt64( resultType.GetBasicType() ); | 
|---|
| 1217 |  | 
|---|
| 1218 | //pop ecx | 
|---|
| 1219 | compiler.codeGenerator.op_pop(REG_ECX); | 
|---|
| 1220 |  | 
|---|
| 1221 | //pop ebx | 
|---|
| 1222 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 1223 |  | 
|---|
| 1224 | //pop eax | 
|---|
| 1225 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 1226 |  | 
|---|
| 1227 | //mov dword ptr[eax],ecx | 
|---|
| 1228 | compiler.codeGenerator.op_mov_MR( sizeof(long), REG_ECX, REG_EAX, 0, MOD_BASE ); | 
|---|
| 1229 |  | 
|---|
| 1230 | //mov dword ptr[eax+sizeof(long)],ebx | 
|---|
| 1231 | compiler.codeGenerator.op_mov_MR( sizeof(long), REG_EBX, REG_EAX, 0x04, MOD_BASE_DISP8 ); | 
|---|
| 1232 | } | 
|---|
| 1233 | else if(type==DEF_DWORD){ | 
|---|
| 1234 | ChangeTypeToLong( resultType.GetBasicType() ); | 
|---|
| 1235 |  | 
|---|
| 1236 | //pop ebx | 
|---|
| 1237 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 1238 |  | 
|---|
| 1239 | //pop eax | 
|---|
| 1240 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 1241 |  | 
|---|
| 1242 | //mov dword ptr[eax],ebx | 
|---|
| 1243 | compiler.codeGenerator.op_mov_MR( sizeof(long), REG_EBX, REG_EAX, 0, MOD_BASE ); | 
|---|
| 1244 | } | 
|---|
| 1245 | else if(type==DEF_WORD){ | 
|---|
| 1246 | ChangeTypeToLong( resultType.GetBasicType() ); | 
|---|
| 1247 |  | 
|---|
| 1248 | //pop ebx | 
|---|
| 1249 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 1250 |  | 
|---|
| 1251 | //pop eax | 
|---|
| 1252 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 1253 |  | 
|---|
| 1254 | //mov word ptr[eax],bx | 
|---|
| 1255 | compiler.codeGenerator.op_mov_MR( sizeof(short), REG_EBX, REG_EAX, 0, MOD_BASE ); | 
|---|
| 1256 | } | 
|---|
| 1257 | else if(type==DEF_BYTE){ | 
|---|
| 1258 | ChangeTypeToLong( resultType.GetBasicType() ); | 
|---|
| 1259 |  | 
|---|
| 1260 | //pop ebx | 
|---|
| 1261 | compiler.codeGenerator.op_pop(REG_EBX); | 
|---|
| 1262 |  | 
|---|
| 1263 | //pop eax | 
|---|
| 1264 | compiler.codeGenerator.op_pop(REG_EAX); | 
|---|
| 1265 |  | 
|---|
| 1266 | //mov byte ptr[eax],bl | 
|---|
| 1267 | compiler.codeGenerator.op_mov_MR( sizeof(char), REG_EBX, REG_EAX, 0, MOD_BASE ); | 
|---|
| 1268 | } | 
|---|
| 1269 | } | 
|---|