source: dev/trunk/ab5.0/abdev/compiler_x64/Compile_Statement.cpp@ 676

Last change on this file since 676 was 676, checked in by dai_9181, 16 years ago

If/While/Doなどのステートメントに引き渡す式の戻り値がクラス型の場合はBoolean型へのキャストを試みるようにした。

File size: 24.3 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4
5#include "../BasicCompiler_Common/common.h"
6#include "Opcode.h"
7
8void OpcodeOthers(const char *Command){
9 int i,i2;
10
11 char leftTerm[8192];
12 int lastParePos = 0;
13 for(i=0;;i++){
14 if(Command[i]=='\"'){
15 //ダブルクォートは不正なのでエラー扱い
16 leftTerm[i]=0;
17 compiler.errorMessenger.Output(3,leftTerm,cp);
18 return;
19 }
20
21 if(Command[i]=='('){
22 lastParePos = i;
23 i2=GetStringInPare(leftTerm+i,Command+i);
24 i+=i2-1;
25 continue;
26 }
27 if(Command[i]=='['){
28 i2=GetStringInBracket(leftTerm+i,Command+i);
29 i+=i2-1;
30 continue;
31 }
32 if(Command[i]=='\0'){
33 leftTerm[i] = 0;
34 break;
35 }
36
37 if( IsNumCalcMark( Command, i ) ){
38 leftTerm[i] = 0;
39 break;
40 }
41
42 leftTerm[i]=Command[i];
43 }
44 if(!(
45 IsVariableTopChar(leftTerm[0])||
46 leftTerm[0]=='.'||
47 (leftTerm[0]==1&&leftTerm[1]==ESC_PSMEM)
48 )){
49 compiler.errorMessenger.Output(1,NULL,cp);
50 return;
51 }
52
53
54 if(Command[i]=='\0' && lastParePos == 0){
55 //////////////////////////////
56 // パラメータ無しのマクロ検索
57 //////////////////////////////
58
59 const UserProc *pUserProc=GetSubHash(Command);
60
61 //GetSubHash内でエラー提示が行われた場合
62 if(pUserProc==(UserProc *)-1) return;
63
64 if(pUserProc==0){
65 char temporary[VN_SIZE];
66 lstrcpy(temporary,Command);
67
68 CharUpper(temporary);
69 pUserProc=GetSubHash(temporary);
70
71 //GetSubHash内でエラー提示が行われた場合
72 if(pUserProc==(UserProc *)-1) return;
73 }
74
75 if(pUserProc){
76 if( !pUserProc->IsMacro() ){
77 compiler.errorMessenger.Output(10,Command,cp);
78 }
79
80 Opcode_CallProc("",pUserProc,0,"");
81
82 return;
83 }
84 }
85 else if(IsNumCalcMark(Command,i)){
86 //代入演算
87 OpcodeCalc(Command);
88 return;
89 }
90
91 if( pobj_reg ){
92 compiler.errorMessenger.OutputFatalError();
93 }
94 pobj_reg=new CRegister(REG_RAX);
95
96 Type resultType;
97 bool isLiteral, isNeedHeapFreeStructure = false;
98 bool result = TermOpe( leftTerm, Type(), resultType, isLiteral, isNeedHeapFreeStructure, NULL, true );
99
100 delete pobj_reg;
101 pobj_reg = NULL;
102
103 if( result ){
104
105 /////////////////////
106 // 戻り値の処理
107 /////////////////////
108
109 if( resultType.IsStruct() ){
110 //mov r14,rax
111 compiler.codeGenerator.op_mov_RR(REG_R14,REG_RAX);
112
113 FreeTempObject(REG_R14,&resultType.GetClass());
114 }
115
116 return;
117 }
118
119 // どこにも当てはまらなかったため、失敗
120 compiler.errorMessenger.Output(1,NULL,cp);
121}
122
123void Judgment(char *buffer){
124 int reg=REG_RAX;
125 Type resultType;
126 bool isNeedHeapFreeStructure;
127 if( !NumOpe(&reg,buffer,Type(DEF_BOOLEAN),resultType, &isNeedHeapFreeStructure) ){
128 return;
129 }
130
131 if( resultType.IsObject() )
132 {
133 // Boolean型にキャストする
134 Type booleanType( DEF_BOOLEAN );
135 CallCastOperatorProc( reg, resultType, isNeedHeapFreeStructure, booleanType );
136 resultType = booleanType;
137 }
138
139 int offset;
140
141 if(resultType.IsDouble()){
142 double dbl=0;
143 offset=compiler.GetObjectModule().dataTable.Add( dbl );
144
145 //comisd xmm0,qword ptr[data table offset]
146 compiler.codeGenerator.PutOld(
147 (char)0x66,
148 (char)0x0F,
149 (char)0x2F,
150 (char)0x04,
151 (char)0x25
152 );
153 compiler.codeGenerator.PutOld(
154 (long)offset,
155 Schedule::DataTable
156 );
157 }
158 else if(resultType.IsSingle()){
159 float flt=0;
160 offset=compiler.GetObjectModule().dataTable.Add( flt );
161
162 //comiss xmm0,dword ptr[data table offset]
163 compiler.codeGenerator.PutOld(
164 (char)0x0F,
165 (char)0x2F,
166 (char)0x04,
167 (char)0x25
168 );
169 compiler.codeGenerator.PutOld(
170 (long)offset,
171 Schedule::DataTable
172 );
173 }
174 else{
175 //整数型
176
177 //cmp rax,0
178 compiler.codeGenerator.op_cmp_value(resultType.GetSize(),REG_RAX,0);
179 }
180}
181
182void OpcodeIf(char *Parameter){
183 for(int i=0;;i++){
184 if(Parameter[i]=='\0'){
185 compiler.errorMessenger.Output(21,NULL,cp);
186 return;
187 }
188 if(Parameter[i]==1&&Parameter[i+1]==ESC_THEN){
189 Parameter[i]=0;
190 break;
191 }
192 }
193
194 //条件式を実行してフラグをセット
195 Judgment(Parameter);
196
197 //je (endif、または else まで条件ジャンプ)
198 const PertialSchedule *pIfPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
199
200
201 /////////////////////////
202 // If内をコード化
203 /////////////////////////
204
205 //レキシカルスコープをレベルアップ
206 compiler.codeGenerator.lexicalScopes.Start(
207 compiler.codeGenerator.GetNativeCodeSize(),
208 LexicalScope::SCOPE_TYPE_IF
209 );
210
211 int i2=CompileBuffer(ESC_ENDIF,0);
212
213 //レキシカルスコープをレベルダウン
214 compiler.codeGenerator.lexicalScopes.End();
215
216
217 if(i2==ESC_ELSE){
218 //jmp (endifまで)
219 const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
220
221 compiler.codeGenerator.opfix_JmpPertialSchedule( pIfPertialSchedule );
222
223
224
225 /////////////////////////
226 // Else内をコード化
227 /////////////////////////
228
229 //レキシカルスコープをレベルアップ
230 compiler.codeGenerator.lexicalScopes.Start(
231 compiler.codeGenerator.GetNativeCodeSize(),
232 LexicalScope::SCOPE_TYPE_IF
233 );
234
235 CompileBuffer(ESC_ENDIF,0);
236
237 //レキシカルスコープをレベルダウン
238 compiler.codeGenerator.lexicalScopes.End();
239
240
241 compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule );
242 }
243 else{
244 compiler.codeGenerator.opfix_JmpPertialSchedule( pIfPertialSchedule );
245 }
246}
247
248int GetLabelAddress(char *LabelName,int LineNum){
249 if(LabelName){
250 BOOST_FOREACH( const GotoLabel &label, compiler.codeGenerator.gotoLabels )
251 {
252 if( label.name.size() > 0 )
253 {
254 if( label.name == LabelName )
255 {
256 return label.address;
257 }
258 }
259 }
260 }
261 else{
262 BOOST_FOREACH( const GotoLabel &label, compiler.codeGenerator.gotoLabels )
263 {
264 if( label.name.size() == 0 )
265 {
266 if( label.line == LineNum )
267 {
268 return label.address;
269 }
270 }
271 }
272 }
273 return -1;
274}
275void OpcodeGoto(char *Parameter){
276 extern HANDLE hHeap;
277 int i,LineNum;
278
279 if(Parameter[0]=='*'){
280 i=GetLabelAddress(Parameter+1,0);
281
282 if( i == -1 )
283 {
284 //jmp ...(schedule)
285 compiler.codeGenerator.op_jmp_goto_schedule( (const std::string)(Parameter + 1), 0, cp );
286 }
287 else
288 {
289 //jmp ...
290 compiler.codeGenerator.op_jmp(
291 i-compiler.codeGenerator.GetNativeCodeSize(),
292 sizeof(long),
293 false,
294 true
295 );
296 }
297 }
298 else{
299 LineNum=atoi(Parameter);
300 i=GetLabelAddress(0,LineNum);
301
302 if( i == -1 )
303 {
304 //jmp ...(schedule)
305 compiler.codeGenerator.op_jmp_goto_schedule( "", LineNum, cp );
306 }
307 else
308 {
309 //jmp ...
310 compiler.codeGenerator.op_jmp(
311 i-compiler.codeGenerator.GetNativeCodeSize(),
312 sizeof(long),
313 false,
314 true
315 );
316 }
317 }
318}
319void OpcodeWhile(char *Parameter){
320 extern HANDLE hHeap;
321
322 //Continueアドレスのバックアップとセット
323 compiler.codeGenerator.ContinueAreaBegin();
324
325 if(!Parameter[0]) compiler.errorMessenger.Output(10,"While",cp);
326
327 //条件式を実行してフラグをセット
328 Judgment(Parameter);
329
330 //je (Wend まで)
331 const PertialSchedule *pWhilePertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
332
333 //レキシカルスコープをレベルアップ
334 compiler.codeGenerator.lexicalScopes.Start( compiler.codeGenerator.GetNativeCodeSize(), LexicalScope::SCOPE_TYPE_WHILE );
335
336 //While内をコンパイル
337 CompileBuffer(0,COM_WEND);
338
339 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
340
341 //jmp ...
342 compiler.codeGenerator.op_jmp_continue();
343
344 //レキシカルスコープをレベルダウン
345 compiler.codeGenerator.lexicalScopes.End();
346
347 compiler.codeGenerator.opfix_JmpPertialSchedule( pWhilePertialSchedule );
348
349 //Continueアドレスを復元
350 compiler.codeGenerator.ContinueAreaEnd();
351}
352
353char szNextVariable[VN_SIZE];
354void OpcodeFor(char *Parameter){
355 extern HANDLE hHeap;
356 Type resultType;
357 int i,i2;
358 char temporary[VN_SIZE],variable[VN_SIZE],JudgeNum[VN_SIZE],StepNum[VN_SIZE];
359 bool isError = false;
360
361 //第1パラメータを取得
362 i=GetOneParameter(Parameter,0,temporary);
363 if(!Parameter[i]){
364 compiler.errorMessenger.Output(12,"For",cp);
365 isError = true;
366 goto ErrorStep;
367 }
368
369 for(i2=0;;i2++){
370 if(temporary[i2]=='='){
371 variable[i2]=0;
372
373 //カウンタ初期化
374 OpcodeCalc(temporary);
375 break;
376 }
377 if(temporary[i2]=='\0'){
378 compiler.errorMessenger.Output(12,"For",cp);
379 isError = true;
380 goto ErrorStep;
381 }
382 variable[i2]=temporary[i2];
383 }
384
385 //jmp ...
386 const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
387
388 //Continueアドレスのバックアップとセット
389 compiler.codeGenerator.ContinueAreaBegin();
390
391 //第2パラメータを取得(to~)
392 i=GetOneParameter(Parameter,i,JudgeNum);
393
394 //第3パラメータを取得(step~)
395 if(Parameter[i]){
396 i=GetOneParameter(Parameter,i,StepNum);
397 if(Parameter[i]) compiler.errorMessenger.Output(12,"For",cp);
398 }
399 else lstrcpy(StepNum,"1");
400
401 //カウンタを増加させる
402 sprintf(temporary,"%s=(%s)+(%s)",variable,variable,StepNum);
403 OpcodeCalc(temporary);
404
405 compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule );
406
407 //増加か減少かを区別する
408 sprintf(temporary,"(%s)>=0",StepNum);
409
410 int reg;
411 reg=REG_RAX;
412 if( !NumOpe(&reg,temporary,Type(),resultType) ){
413 return;
414 }
415
416 //cmp rax,0
417 compiler.codeGenerator.op_cmp_value(resultType.GetSize(),REG_RAX,0);
418
419 //je [カウンタ減少の場合の判定]
420 pTempPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
421
422 //判定(カウンタ増加の場合)
423 sprintf(temporary,"%s<=(%s)",variable,JudgeNum);
424
425 reg=REG_RAX;
426 NumOpe(&reg,temporary,Type(),Type());
427
428 //jmp [カウンタ減少の場合の判定を飛び越す]
429 const PertialSchedule *pTempPertialSchedule2 = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
430
431 //jeジャンプ先のオフセット値
432 compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule );
433
434 //判定(カウンタ減少の場合)
435 sprintf(temporary,"%s>=(%s)",variable,JudgeNum);
436
437 reg=REG_RAX;
438 NumOpe(&reg,temporary,Type(),resultType);
439
440 //jmpジャンプ先のオフセット値
441 compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule2 );
442
443 //cmp rax,0
444 compiler.codeGenerator.op_cmp_value(resultType.GetSize(),REG_RAX,0);
445
446ErrorStep:
447
448 //je ...
449 pTempPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
450
451 //レキシカルスコープをレベルアップ
452 compiler.codeGenerator.lexicalScopes.Start( compiler.codeGenerator.GetNativeCodeSize(), LexicalScope::SCOPE_TYPE_FOR );
453
454 //For内をコンパイル
455 CompileBuffer(0,COM_NEXT);
456
457 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
458
459 if(szNextVariable[0]){
460 if(lstrcmp(szNextVariable,variable)!=0){
461 compiler.errorMessenger.Output(55,szNextVariable,cp);
462 }
463 }
464
465 //jmp ...
466 compiler.codeGenerator.op_jmp_continue();
467
468 //レキシカルスコープをレベルダウン
469 compiler.codeGenerator.lexicalScopes.End();
470
471 //jeジャンプ先のオフセット値
472 compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule );
473
474 //Continueアドレスを復元
475 compiler.codeGenerator.ContinueAreaEnd();
476}
477
478void OpcodeForeach( const char *Parameter )
479{
480 Type resultType;
481 char temporary[VN_SIZE],variable[VN_SIZE],collectionVar[VN_SIZE];
482 bool isError = false;
483
484 //レキシカルスコープをレベルアップ
485 compiler.codeGenerator.lexicalScopes.Start( compiler.codeGenerator.GetNativeCodeSize(), LexicalScope::SCOPE_TYPE_FOR );
486
487 //第1パラメータを取得
488 int i = 0;
489 GetCustomToken( variable, Parameter, i, ESC_IN, true );
490 if(!Parameter[i]){
491 compiler.errorMessenger.Output(12,"Foreach",cp);
492 isError = true;
493 goto ErrorStep;
494 }
495 i++;
496
497 //第2パラメータを取得(in~)
498 lstrcpy( collectionVar, Parameter + i );
499
500 if( !GetVarType( variable, resultType, false ) )
501 {
502 Type collectionType;
503 if( !NumOpe_GetType( collectionVar, Type(), collectionType ) )
504 {
505 isError = true;
506 goto ErrorStep;
507 }
508
509 // 未定義の場合は自動的に定義する
510 sprintf(temporary,"%s=Nothing%c%c%s", variable, 1, ESC_AS, collectionType.GetActualGenericType(0).GetClass().GetFullName().c_str() );
511 OpcodeDim(temporary,DIMFLAG_INITDEBUGVAR);
512 }
513
514 // Resetメソッドを呼び出す
515 sprintf( temporary, "%s.Reset()", collectionVar );
516 Compile( temporary );
517
518 //Continueアドレスのバックアップとセット
519 compiler.codeGenerator.ContinueAreaBegin();
520
521 // MoveNextメソッドを呼び出す
522 sprintf( temporary, "%s.MoveNext()", collectionVar );
523 int reg = REG_RAX;
524 NumOpe(&reg,temporary,Type(),resultType);
525
526 //cmp rax,0
527 compiler.codeGenerator.op_cmp_value(resultType.GetSize(),REG_RAX,0);
528
529ErrorStep:
530
531 //je ...
532 const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(long), true );
533
534 if( !isError )
535 {
536 // Currentプロパティから現在の値を取得
537 sprintf( temporary, "%s=%s.Current", variable, collectionVar );
538 Compile( temporary );
539 }
540
541 //For内をコンパイル
542 CompileBuffer(0,COM_NEXT);
543
544 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
545
546 if(szNextVariable[0]){
547 if(lstrcmp(szNextVariable,variable)!=0){
548 compiler.errorMessenger.Output(55,szNextVariable,cp);
549 }
550 }
551
552 if( !isError )
553 {
554 //jmp ...
555 compiler.codeGenerator.op_jmp_continue();
556 }
557
558 //レキシカルスコープをレベルダウン
559 compiler.codeGenerator.lexicalScopes.End();
560
561 //jeジャンプ先のオフセット値
562 compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule );
563
564 //Continueアドレスを復元
565 compiler.codeGenerator.ContinueAreaEnd();
566}
567
568void OpcodeDo(char *Parameter){
569 extern HANDLE hHeap;
570 int i,i2,i3;
571
572 if(Parameter[0]) compiler.errorMessenger.Output(10,"Do",cp);
573
574 //Continueアドレスのバックアップとセット
575 compiler.codeGenerator.ContinueAreaBegin();
576
577 //レキシカルスコープをレベルアップ
578 compiler.codeGenerator.lexicalScopes.Start( compiler.codeGenerator.GetNativeCodeSize(), LexicalScope::SCOPE_TYPE_DO );
579
580 //Do内をコンパイル
581 CompileBuffer(0,COM_LOOP);
582
583 compiler.codeGenerator.lexicalScopes.CallDestructorsOfScopeEnd();
584
585 const PertialSchedule *pDoPertialSchedule = NULL;
586
587 extern char *basbuf;
588 char temporary[VN_SIZE];
589 for(i=cp-1;;i--){
590 if(IsCommandDelimitation(basbuf[i])){
591 i+=3;
592 if(!(basbuf[i]=='0'||basbuf[i]=='1')){
593 //無条件ループ
594 break;
595 }
596 i3=i;
597
598 for(i+=2,i2=0;;i++,i2++){
599 if(IsCommandDelimitation(basbuf[i])){
600 temporary[i2]=0;
601 break;
602 }
603 temporary[i2]=basbuf[i];
604 }
605
606 //条件式を実行してフラグをセット
607 Judgment(temporary);
608
609 if(basbuf[i3]=='0'){
610 //While
611
612 //je 5(ループ終了)
613 pDoPertialSchedule = compiler.codeGenerator.op_je( 0, sizeof(char), true );
614 }
615 else if(basbuf[i3]=='1'){
616 //Until
617
618 //jne 5(ループ終了)
619 pDoPertialSchedule = compiler.codeGenerator.op_jne( 0, sizeof(char), true );
620 }
621 break;
622 }
623 }
624
625 //jmp ...
626 compiler.codeGenerator.op_jmp_continue();
627
628 if( pDoPertialSchedule )
629 {
630 compiler.codeGenerator.opfix_JmpPertialSchedule( pDoPertialSchedule );
631 }
632
633 //jmp ...
634 const PertialSchedule *pTempPertialSchedule = compiler.codeGenerator.op_jmp( 0, sizeof(long), true );
635
636 //レキシカルスコープをレベルダウン
637 compiler.codeGenerator.lexicalScopes.End();
638
639 //jmpジャンプ先のオフセット値
640 compiler.codeGenerator.opfix_JmpPertialSchedule( pTempPertialSchedule );
641
642 //Continueアドレスを復元
643 compiler.codeGenerator.ContinueAreaEnd();
644}
645void OpcodeContinue(void){
646 //jmp ...(Continue addr)
647 compiler.codeGenerator.op_jmp_continue();
648}
649
650void OpcodeExitSub(void){
651 if( compiler.IsGlobalAreaCompiling() ){
652 compiler.errorMessenger.Output(12,"Exit Sub/Function",cp);
653 return;
654 }
655
656 //未解放のローカルオブジェクトのデストラクタを呼び出す
657 compiler.codeGenerator.lexicalScopes.CallDestructorsOfReturn();
658
659 //jmp ...(End Sub/Function)
660 compiler.codeGenerator.op_jmp_exitsub();
661}
662
663//Caseスケジュール
664class SelectSchedule
665{
666public:
667 SelectSchedule( int typeSize )
668 : typeSize( typeSize )
669 , nowCaseSchedule( 0 )
670 {
671 }
672
673 PertialSchedules casePertialSchedules;
674 int typeSize;
675 int nowCaseSchedule;
676};
677std::vector<SelectSchedule> selectSchedules;
678
679void OpcodeSelect( const char *lpszParms )
680{
681 extern HANDLE hHeap;
682 extern char *basbuf;
683 int i,i2,i3,NowCaseCp;
684 char temporary[VN_SIZE];
685
686 int reg1=REG_RAX;
687 Type type1;
688 bool result = NumOpe(&reg1,lpszParms,Type(), type1 );
689
690 selectSchedules.push_back( SelectSchedule( type1.GetSize() ) );
691
692 if( result )
693 {
694 if( selectSchedules.back().typeSize < sizeof(long) ){
695 selectSchedules.back().typeSize = sizeof(long);
696 }
697
698 if(type1.IsDouble()){
699 //movsd qword ptr[rsp+offset],xmm_reg ※スタックフレームを利用
700 pobj_sf->push(reg1,sizeof(double));
701 }
702 else if(type1.IsSingle()){
703 //movss dword ptr[rsp+offset],xmm_reg ※スタックフレームを利用
704 pobj_sf->push(reg1,sizeof(float));
705 }
706 else{
707 ExtendTypeTo64(type1.GetBasicType(),reg1);
708
709 //mov qword ptr[rsp+offset],reg ※スタックフレームを利用
710 pobj_sf->push(reg1);
711 }
712
713 for(i=cp;;i++){
714 if(basbuf[i]=='\0'){
715 selectSchedules.pop_back();
716 compiler.errorMessenger.Output(22,"Select",cp);
717 return;
718 }
719 if(basbuf[i]==1&&basbuf[i+1]==ESC_SELECTCASE){
720 for(i2=0;;i++){
721 if(basbuf[i]==1&&basbuf[i+1]==ESC_SELECTCASE) i2++;
722 if(basbuf[i]==1&&basbuf[i+1]==ESC_ENDSELECT){
723 i2--;
724 if(i2==0) break;
725 }
726 }
727 continue;
728 }
729 if(basbuf[i]==1&&basbuf[i+1]==ESC_ENDSELECT) break;
730
731 if(basbuf[i]==1&&basbuf[i+1]==ESC_CASE){
732 NowCaseCp=i;
733
734 i++;
735 while(1){
736 for(i++,i2=0;;i++,i2++){
737 if(basbuf[i]=='\"'){
738 i3=GetStringInQuotation(temporary+i2,basbuf+i);
739 i+=i3-1;
740 i2+=i3-1;
741 continue;
742 }
743 if(basbuf[i]=='('){
744 i3=GetStringInPare(temporary+i2,basbuf+i);
745 i+=i3-1;
746 i2+=i3-1;
747 continue;
748 }
749 if(basbuf[i]=='['){
750 i3=GetStringInBracket(temporary+i2,basbuf+i);
751 i+=i3-1;
752 i2+=i3-1;
753 continue;
754 }
755
756 if(IsCommandDelimitation(basbuf[i])){
757 temporary[i2]=0;
758 break;
759 }
760 if(basbuf[i]==','){
761 temporary[i2]=0;
762 break;
763 }
764
765 temporary[i2]=basbuf[i];
766 }
767
768 //エラー用
769 i2=cp;
770 cp=NowCaseCp;
771
772 int reg2=REG_RDX;
773 Type type2;
774 if( !NumOpe(&reg2,temporary,type1,type2) ){
775 return;
776 }
777
778 cp=i2;
779
780 if(type1.IsObject()){
781 std::vector<const UserProc *> subs;
782 type1.GetClass().GetDynamicMethods().Enum( CALC_EQUAL, subs );
783 if( subs.size() == 0 ){
784 return;
785 }
786
787 Parameters params;
788 params.push_back( new Parameter( "", Type( type2 ) ) );
789
790 //オーバーロードを解決
791 const UserProc *pUserProc = OverloadSolution( "==", subs, params, Type( DEF_BOOLEAN ), type1 );
792
793 delete params[0];
794
795 if(!pUserProc){
796 //エラー
797 return;
798 }
799
800
801 //実体オブジェクト
802 if(reg2!=REG_RDX){
803 //mov rdx,reg2
804 compiler.codeGenerator.op_mov_RR(REG_RDX,reg2);
805 }
806
807 //mov rcx,qword ptr[rsp+offset] ※スタックフレームから参照
808 pobj_sf->ref(REG_RCX);
809
810 //call operator_proc ※ ==演算子
811 compiler.codeGenerator.op_call(pUserProc);
812
813 //test rax,rax
814 compiler.codeGenerator.op_test(REG_RAX,REG_RAX);
815
816 //jne ...
817 selectSchedules.back().casePertialSchedules.push_back(
818 compiler.codeGenerator.op_jne( 0, sizeof(long), true )
819 );
820 }
821 else{
822 if(type1.IsDouble()){
823 int xmm_reg;
824 if(IsXmmReg(reg2)) xmm_reg=reg2;
825 else xmm_reg=REG_XMM5;
826 ChangeTypeToXmm_Double(type2.GetBasicType(),xmm_reg,reg2);
827
828 //movsd xmm4,qword ptr[rsp+offset] ※スタックフレームから参照
829 pobj_sf->ref(REG_XMM4,sizeof(double));
830
831 //comiss xmm_reg1,xmm_reg2
832 compiler.codeGenerator.op_comisd(xmm_reg,REG_XMM4);
833 }
834 else if(type1.IsSingle()){
835 int xmm_reg;
836 if(IsXmmReg(reg2)) xmm_reg=reg2;
837 else xmm_reg=REG_XMM5;
838 ChangeTypeToXmm_Single(type2.GetBasicType(),xmm_reg,reg2);
839
840 //movss xmm4,dword ptr[rsp+offset] ※スタックフレームから参照
841 pobj_sf->ref(REG_XMM4,sizeof(float));
842
843 //comiss xmm_reg1,xmm_reg2
844 compiler.codeGenerator.op_comiss(xmm_reg,REG_XMM4);
845 }
846 else{
847 //その他整数型
848
849 i2=NeutralizationType(type1.GetBasicType(),-1,type2.GetBasicType(),-1);
850
851 //mov r14,qword ptr[rsp+offset] ※スタックフレームから参照
852 pobj_sf->ref(REG_R14);
853
854 //cmp reg2,r14
855 compiler.codeGenerator.op_cmp_reg(Type(i2).GetSize(),reg2,REG_R14);
856 }
857
858 //je ...
859 selectSchedules.back().casePertialSchedules.push_back(
860 compiler.codeGenerator.op_je( 0, sizeof(long), true )
861 );
862 }
863
864 if(basbuf[i]!=',') break;
865 }
866 }
867 if(basbuf[i]==1&&basbuf[i+1]==ESC_CASEELSE){
868 //jmp ...
869 selectSchedules.back().casePertialSchedules.push_back(
870 compiler.codeGenerator.op_jmp( 0, sizeof(long), true )
871 );
872 }
873 }
874
875 //スタックフレームを1スペースだけ解除
876 pobj_sf->pop(REG_NON);
877 }
878
879 //レキシカルスコープをレベルアップ
880 compiler.codeGenerator.lexicalScopes.Start( compiler.codeGenerator.GetNativeCodeSize(), LexicalScope::SCOPE_TYPE_SELECT );
881
882 //Select Case内をコンパイル
883 CompileBuffer(ESC_ENDSELECT,0);
884
885 //jmp EndSelect
886 selectSchedules.back().casePertialSchedules.push_back(
887 compiler.codeGenerator.op_jmp( 0, sizeof(long), true )
888 );
889
890 //最終スケジュール
891 for(i=selectSchedules.back().nowCaseSchedule;i<(int)selectSchedules.back().casePertialSchedules.size();i++){
892 compiler.codeGenerator.opfix_JmpPertialSchedule( selectSchedules.back().casePertialSchedules[i] );
893 }
894
895 //レキシカルスコープをレベルダウン
896 compiler.codeGenerator.lexicalScopes.End();
897
898 selectSchedules.pop_back();
899}
900void OpcodeCase(char *Parameter){
901 int i;
902
903 if(selectSchedules.back().typeSize==-1){
904 compiler.errorMessenger.Output(30,"Case",cp);
905 return;
906 }
907
908 //jmp EndSelect
909 selectSchedules.back().casePertialSchedules.push_back(
910 compiler.codeGenerator.op_jmp( 0, sizeof(long), true )
911 );
912
913 i=0;
914 while(1){
915 //Caseスケジュール
916 compiler.codeGenerator.opfix_JmpPertialSchedule( selectSchedules.back().casePertialSchedules[selectSchedules.back().nowCaseSchedule] );
917 selectSchedules.back().nowCaseSchedule++;
918
919 i=JumpOneParameter(Parameter,i);
920 if(Parameter[i]=='\0') break;
921 }
922}
923
924void OpcodeGosub(char *Parameter){
925 compiler.errorMessenger.Output(-1,"Gosub ~ Returnステートメントは64ビットコンパイラで利用することはできません。",cp);
926}
927void OpcodeReturn(char *Parameter){
928 if( compiler.IsGlobalAreaCompiling() ){
929 compiler.errorMessenger.Output(62,NULL,cp);
930 }
931 else{
932 //戻り値をセット
933 if(Parameter[0]){
934 const UserProc &proc = compiler.GetCompilingUserProc();
935
936 const char *temp = "_System_ReturnValue";
937 if(proc.GetName()[0]==1&&proc.GetName()[1]==ESC_OPERATOR)
938 {
939 }
940 else{
941 temp=proc.GetName().c_str();
942 }
943
944 char temporary[VN_SIZE];
945 sprintf(temporary,"%s=%s",temp,Parameter);
946 OpcodeCalc(temporary);
947 }
948
949 //プロシージャを抜け出す(C言語のreturnと同様の処理を行う)
950 OpcodeExitSub();
951 }
952}
953
954
955////////////
956// ポインタ
957////////////
958
959void OpcodeSetPtrData(char *Parameter,int type){
960 int i;
961 char temporary[VN_SIZE];
962
963 if(Parameter[0]=='('){
964 i=JumpStringInPare(Parameter,1);
965 if(Parameter[i+1]=='\0'){
966 for(i=0;;i++){
967 Parameter[i]=Parameter[i+1];
968 if(Parameter[i]=='\0') break;
969 }
970 Parameter[i-1]=0;
971 }
972 }
973
974 //第1パラメータを取得
975 i=GetOneParameter(Parameter,0,temporary);
976 if(!Parameter[i]){
977 compiler.errorMessenger.Output(1,NULL,cp);
978 return;
979 }
980
981 int reg_ptr=REG_RAX;
982 Type resultType;
983 if( !NumOpe(&reg_ptr,temporary,Type(),resultType) ){
984 return;
985 }
986 if(!resultType.IsWhole()){
987 compiler.errorMessenger.Output(11,Parameter,cp);
988 return;
989 }
990
991 //結果を格納しているレジスタをブロッキング
992 pobj_BlockReg->lock(reg_ptr);
993
994 //第2パラメータを取得
995 i=GetOneParameter(Parameter,i,temporary);
996 if(Parameter[i]){
997 compiler.errorMessenger.Output(1,NULL,cp);
998 return;
999 }
1000
1001 int temp_reg=REG_NON;
1002 if( !NumOpe(&temp_reg,temporary,Type(),resultType) ){
1003 return;
1004 }
1005
1006 //レジスタのブロッキングを解除
1007 pobj_BlockReg->clear();
1008
1009 if(type==DEF_DOUBLE){
1010 ChangeTypeToXmm_Double(resultType.GetBasicType(),REG_XMM0,temp_reg);
1011
1012 //movsd qword ptr[reg_ptr],xmm0
1013 compiler.codeGenerator.op_movsd_MR(REG_XMM0,reg_ptr,0,MOD_BASE);
1014 }
1015 else if(type==DEF_SINGLE){
1016 ChangeTypeToXmm_Single(resultType.GetBasicType(),REG_XMM0,temp_reg);
1017
1018 //movss dword ptr[reg_ptr],xmm0
1019 compiler.codeGenerator.op_movss_MR(REG_XMM0,reg_ptr,0,MOD_BASE);
1020 }
1021 else{
1022 ChangeTypeToWhole(resultType,Type(type),REG_RCX,temp_reg);
1023
1024 //mov ptr[reg_ptr],rcx
1025 compiler.codeGenerator.op_mov_MR(Type(type).GetSize(),REG_RCX,reg_ptr,0,MOD_BASE);
1026 }
1027}
Note: See TracBrowser for help on using the repository browser.