source: dev/trunk/abdev/BasicCompiler_Common/src/CommonCodeGenerator.cpp@ 246

Last change on this file since 246 was 246, checked in by dai_9181, 17 years ago
File size: 5.7 KB
Line 
1#include "stdafx.h"
2
3#include <CodeGenerator.h>
4
5
6void CodeGenerator::CheckUnresolveSchedule()
7{
8 if( pertialSchedules.size() > 0 )
9 {
10 SetError();
11 }
12}
13
14
15// 分岐関連
16void CodeGenerator::opfix_JmpPertialSchedule( const PertialSchedule *pPertialSchedule )
17{
18 bool isSuccessful = false;
19
20 CodeGenerator::PertialSchedules::iterator it = pertialSchedules.begin();
21 while( it != pertialSchedules.end() )
22 {
23 if( (*it) == pPertialSchedule )
24 {
25 long newValue = pNativeCode->GetSize() - (pPertialSchedule->GetCodePos()+pPertialSchedule->GetTypeSize());
26
27 extern int obp;
28 long newValueOld = obp - (pPertialSchedule->GetObpOld()+pPertialSchedule->GetTypeSize());
29
30 if( pPertialSchedule->GetTypeSize() == sizeof(char) )
31 {
32 if( newValue < -128 || 127 < newValue )
33 {
34 // 範囲外
35 SetError();
36 }
37
38 pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), (char)newValue );
39
40 // TODO: 未完成(用が無くなったら消す)
41 pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), (char)newValueOld );
42 }
43 else if( pPertialSchedule->GetTypeSize() == sizeof(long) )
44 {
45 pNativeCode->Overwrite( pPertialSchedule->GetCodePos(), newValue );
46
47 // TODO: 未完成(用が無くなったら消す)
48 pNativeCode->OverwriteOld( pPertialSchedule->GetObpOld(), newValueOld );
49 }
50 else
51 {
52 SetError();
53 }
54
55 it = pertialSchedules.erase( it );
56 delete pPertialSchedule;
57
58 isSuccessful = true;
59 }
60 else
61 {
62 it++;
63 }
64 }
65
66 if( isSuccessful == false )
67 {
68 SetError();
69 }
70}
71const CodeGenerator::PertialSchedule *CodeGenerator::__jmp_op_format( char opcode, long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
72{
73 long beginCodePos = pNativeCode->GetSize();
74 {
75 // TODO: 未完成
76 extern int obp;
77 beginCodePos = obp;
78 }
79
80 if( opcode == (char)0xEB )
81 {
82 // jmp命令のとき
83 if( op_size == sizeof(char) )
84 {
85 pNativeCode->Put( (char)opcode );
86 }
87 else if( op_size == sizeof(long) )
88 {
89 pNativeCode->Put( (char)0xE9 );
90 }
91 else
92 {
93 SetError();
94 }
95 }
96 else
97 {
98 if( op_size == sizeof(char) )
99 {
100 pNativeCode->Put( (char)((char)0x70 | opcode) );
101 }
102 else if( op_size == sizeof(long) )
103 {
104 pNativeCode->Put( (char)0x0F );
105 pNativeCode->Put( (char)((char)0x80 | opcode) );
106 }
107 else
108 {
109 SetError();
110 }
111 }
112
113 const PertialSchedule *pPertialSchedule = NULL;
114 if( isPertialSchedule )
115 {
116 pertialSchedules.push_back( new PertialSchedule( pNativeCode->GetSize(), op_size ) );
117 pPertialSchedule = pertialSchedules[pertialSchedules.size()-1];
118 }
119
120 if( isSelfOpcodeOffset )
121 {
122 // 自分自身の命令サイズを考慮する場合
123 //offset += ( pNativeCode->GetSize() - beginCodePos ) + op_size;
124
125 // TODO: 未完成
126 extern int obp;
127 offset -= ( obp - beginCodePos ) + op_size;
128 }
129
130 if( op_size == sizeof(char) )
131 {
132 pNativeCode->Put( (char)offset );
133 }
134 else if( op_size == sizeof(long) )
135 {
136 pNativeCode->Put( offset );
137 }
138 else
139 {
140 SetError();
141 }
142
143 return pPertialSchedule;
144}
145const CodeGenerator::PertialSchedule *CodeGenerator::op_jle( long offset, int op_size, bool isPertialSchedule )
146{
147 return __jmp_op_format( (char)0x0E, offset, op_size, isPertialSchedule );
148}
149const CodeGenerator::PertialSchedule *CodeGenerator::op_jbe( long offset, int op_size, bool isPertialSchedule )
150{
151 return __jmp_op_format( (char)0x06, offset, op_size, isPertialSchedule );
152}
153const CodeGenerator::PertialSchedule *CodeGenerator::op_jge( long offset, int op_size, bool isPertialSchedule )
154{
155 return __jmp_op_format( (char)0x0D, offset, op_size, isPertialSchedule );
156}
157const CodeGenerator::PertialSchedule *CodeGenerator::op_jae( long offset, int op_size, bool isPertialSchedule )
158{
159 return __jmp_op_format( (char)0x03, offset, op_size, isPertialSchedule );
160}
161const CodeGenerator::PertialSchedule *CodeGenerator::op_jl( long offset, int op_size, bool isPertialSchedule )
162{
163 return __jmp_op_format( (char)0x0C, offset, op_size, isPertialSchedule );
164}
165const CodeGenerator::PertialSchedule *CodeGenerator::op_jb( long offset, int op_size, bool isPertialSchedule )
166{
167 return __jmp_op_format( (char)0x02, offset, op_size, isPertialSchedule );
168}
169const CodeGenerator::PertialSchedule *CodeGenerator::op_jg( long offset, int op_size, bool isPertialSchedule )
170{
171 return __jmp_op_format( (char)0x0F, offset, op_size, isPertialSchedule );
172}
173const CodeGenerator::PertialSchedule *CodeGenerator::op_ja( long offset, int op_size, bool isPertialSchedule )
174{
175 return __jmp_op_format( (char)0x07, offset, op_size, isPertialSchedule );
176}
177const CodeGenerator::PertialSchedule *CodeGenerator::op_jne( long offset, int op_size, bool isPertialSchedule )
178{
179 return __jmp_op_format( (char)0x05, offset, op_size, isPertialSchedule );
180}
181const CodeGenerator::PertialSchedule *CodeGenerator::op_je( long offset, int op_size, bool isPertialSchedule )
182{
183 return __jmp_op_format( (char)0x04, offset, op_size, isPertialSchedule );
184}
185const CodeGenerator::PertialSchedule *CodeGenerator::op_jmp( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )
186{
187 return __jmp_op_format( (char)0xEB, offset, op_size, isPertialSchedule, isSelfOpcodeOffset );
188}
189void CodeGenerator::op_jmp_continue()
190{
191 //op_jmp( GetContinueCodePos()-(pNativeCode->GetSize()+sizeof(long)), sizeof(long) );
192
193 // TODO: 未完成(OpBuffer/obp廃止が整ったら上記のコードを有効にすべし)
194
195 if( GetContinueCodePosOld() == -1 )
196 {
197 SetError(12,"Continue",cp);
198 return;
199 }
200
201 extern int obp;
202 op_jmp( GetContinueCodePosOld()-obp, sizeof(long), false, true );
203}
204void CodeGenerator::op_jmp_goto_schedule( const GotoLabelSchedule &gotoLabelSchedule )
205{
206 // オペコード
207 pNativeCode->Put( (char)0xE9 );
208
209 gotoLabelSchedules.push_back( gotoLabelSchedule );
210
211 pNativeCode->Put( (long)0 );
212}
Note: See TracBrowser for help on using the repository browser.