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