Changeset 64 in dev for BasicCompiler32/Compile_Var.cpp
- Timestamp:
- Mar 8, 2007, 2:49:34 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
BasicCompiler32/Compile_Var.cpp
r63 r64 23 23 24 24 if(PTR_LEVEL(*pType)){ 25 *pType=MAKE_PTR_TYPE(NATURAL_TYPE(*pType),PTR_LEVEL(*pType)-1); 26 if((*pType)==DEF_OBJECT){ 27 CClass *pClass = (CClass *)lpIndex; 28 int objsize = pClass->GetSize(); 29 30 //imul ebx,objsize 31 OpBuffer[obp++]=(char)0x69; 32 OpBuffer[obp++]=(char)0xDB; 33 *((long *)(OpBuffer+obp))=objsize; 34 obp+=sizeof(long); 35 } 36 else{ 37 i2=GetTypeSize(*pType,-1); 38 if(i2>=2){ 39 //imul ebx,i2 40 OpBuffer[obp++]=(char)0x6B; 41 OpBuffer[obp++]=(char)0xDB; 42 OpBuffer[obp++]=(char)i2; 43 } 25 *pType = PTR_LEVEL_DOWN( *pType ); 26 27 i2=GetTypeSize(*pType,-1); 28 if(i2>=2){ 29 //imul ebx,i2 30 op_imul_RV( REG_EBX, i2 ); 44 31 } 45 32 } … … 56 43 OpBuffer[obp++]=(char)0x03; 57 44 OpBuffer[obp++]=(char)0xCB; 45 } 46 void SetRelativeOffset( RELATIVE_VAR &relativeVar ){ 47 if(relativeVar.dwKind==VAR_DIRECTMEM){ 48 //mov ecx,dword ptr[ecx] 49 op_mov_RM( sizeof(long), REG_ECX, REG_ECX, 0, MOD_BASE ); 50 } 51 else{ 52 //直接参照に切り替え 53 SetVarPtrToEax(&relativeVar); 54 relativeVar.dwKind=VAR_DIRECTMEM; 55 56 //mov ecx,dword ptr[eax] 57 op_mov_RM( sizeof(long), REG_ECX, REG_EAX, 0, MOD_BASE ); 58 } 58 59 } 59 60 BOOL GetArrayOffset(int *SubScripts,char *array,int type,LONG_PTR lpIndex){ … … 168 169 char lpPtrOffset[VN_SIZE]; //第2次配列 169 170 char NestMember[VN_SIZE]; //入れ子メンバ 170 int RefType; //"."参照のときは0、"->"参照のときは1171 CClass::RefType refType; 171 172 lstrcpy(VarName,member); 172 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember, &RefType)) return 0;173 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember,refType)) return 0; 173 174 174 175 //////////////////////////// … … 251 252 //入れ子構造の場合 252 253 253 if(*pType==DEF_OBJECT ){254 if( RefType!=DEF_OBJECT){254 if(*pType==DEF_OBJECT || *pType==DEF_STRUCT){ 255 if( refType != CClass::Dot ){ 255 256 if(isErrorEnabled) SetError(104,member,cp); 256 257 return 0; 257 258 } 258 } 259 else if(*pType==DEF_PTR_OBJECT){ 259 260 if( *pType==DEF_OBJECT ){ 261 // 参照内容へのポインタを抽出 262 SetRelativeOffset( *pRelativeVar ); 263 } 264 } 265 else if(*pType==DEF_PTR_OBJECT||*pType==DEF_PTR_STRUCT){ 260 266 //構造体ポインタ型メンバ変数 261 267 262 268 if(lpPtrOffset[0]){ 263 269 //pObj[n].member 264 if( RefType!=DEF_OBJECT){270 if( ( *pType==DEF_PTR_OBJECT||*pType==DEF_PTR_STRUCT )&& refType != CClass::Dot ){ 265 271 if(isErrorEnabled) SetError(104,member,cp); 266 272 return 0; … … 275 281 else{ 276 282 //pObj->member 277 if( RefType!=DEF_PTR_OBJECT){283 if( ( *pType==DEF_PTR_OBJECT||*pType==DEF_PTR_STRUCT )&& refType != CClass::Pointer ){ 278 284 if(isErrorEnabled) SetError(104,member,cp); 279 285 return 0; 280 286 } 281 287 282 if(pRelativeVar->dwKind==VAR_DIRECTMEM){ 283 //mov ecx,dword ptr[ecx] 284 OpBuffer[obp++]=(char)0x8B; 285 OpBuffer[obp++]=(char)0x09; 286 } 287 else{ 288 //直接参照に切り替え 289 SetVarPtrToEax(pRelativeVar); 290 pRelativeVar->dwKind=VAR_DIRECTMEM; 291 292 //mov ecx,dword ptr[eax] 293 OpBuffer[obp++]=(char)0x8B; 294 OpBuffer[obp++]=(char)0x08; 295 } 296 } 297 } 298 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2)){ 288 SetRelativeOffset( *pRelativeVar ); 289 } 290 } 291 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2) || *pType==MAKE_PTR_TYPE(DEF_STRUCT,2)){ 299 292 //構造体ポインタのポインタ型メンバ変数 300 293 301 294 if(lpPtrOffset[0]){ 302 295 //ppObj[n]->member 303 if( RefType!=DEF_PTR_OBJECT){296 if( refType != CClass::Pointer ){ 304 297 if(isErrorEnabled) SetError(104,member,cp); 305 298 return 0; … … 364 357 BOOL GetVarOffset(bool isErrorEnabled, bool isWriteAccess, const char *NameBuffer, int *pType, RELATIVE_VAR *pRelativeVar, LONG_PTR *plpIndex, int *pss){ 365 358 extern BOOL bCompilingGlobal; 366 int i ,RefType;359 int i; 367 360 LONG_PTR lpIndex; 368 361 char variable[VN_SIZE],member[VN_SIZE],VarName[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE]; … … 374 367 else lstrcpy(variable,NameBuffer); 375 368 376 377 if( FormatUseProcReturnObject( variable, VarName, array, RefType, member ) ){369 CClass::RefType refType; 370 if( FormatUseProcReturnObject( variable, VarName, array, refType, member ) ){ 378 371 // 戻り値オブジェクトのメンバを直接参照しているとき 379 372 //例: func().member … … 410 403 411 404 lstrcpy(VarName,variable); 412 GetVarFormatString(VarName,array,lpPtrOffset,member, &RefType);405 GetVarFormatString(VarName,array,lpPtrOffset,member,refType); 413 406 414 407 int *pSubScripts; … … 546 539 if(member[0]){ 547 540 lstrcpy(temporary,member); 541 548 542 char tempMember[VN_SIZE]; 549 543 char tempArray[VN_SIZE]; 550 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember,&i); 544 { 545 CClass::RefType refType; 546 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember, refType ); 547 } 551 548 552 549 char temp2[VN_SIZE]; … … 665 662 } 666 663 if(member[0]){ 667 if(*pType==DEF_OBJECT ){664 if(*pType==DEF_OBJECT || *pType==DEF_STRUCT){ 668 665 //実態オブジェクトのメンバを参照(obj.member) 669 if( RefType!=DEF_OBJECT){666 if( refType != CClass::Dot ){ 670 667 SetError(104,VarName,cp); 671 668 pRelativeVar->dwKind=NON_VAR; 672 669 return 0; 673 670 } 674 } 675 else if(*pType==DEF_PTR_OBJECT){ 671 672 if( *pType==DEF_OBJECT ){ 673 // 参照内容へのポインタを抽出 674 SetRelativeOffset( *pRelativeVar ); 675 } 676 } 677 else if(*pType==DEF_PTR_OBJECT || *pType==DEF_PTR_STRUCT){ 676 678 //ポインタオブジェクトが示すメンバを参照 677 679 if(lpPtrOffset[0]){ 678 680 //pObj[n].member 679 if( RefType!=DEF_OBJECT){681 if( refType != CClass::Dot ){ 680 682 SetError(104,VarName,cp); 681 683 pRelativeVar->dwKind=NON_VAR; … … 687 689 else{ 688 690 //pObj->member 689 if( RefType!=DEF_PTR_OBJECT){691 if( refType != CClass::Pointer ){ 690 692 SetError(104,VarName,cp); 691 693 pRelativeVar->dwKind=NON_VAR; … … 701 703 } 702 704 } 703 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2) ){705 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2) || *pType==MAKE_PTR_TYPE(DEF_STRUCT,2)){ 704 706 //ポインタオブジェクトが示すメンバを参照 705 707 if(lpPtrOffset[0]){ 706 708 //ppObj[n]->member 707 if( RefType!=DEF_PTR_OBJECT){709 if( refType != CClass::Pointer ){ 708 710 SetError(104,VarName,cp); 709 711 pRelativeVar->dwKind=NON_VAR; … … 785 787 } 786 788 787 if(type==DEF_ OBJECT){789 if(type==DEF_STRUCT){ 788 790 CClass *pobj_c; 789 791 pobj_c=(CClass *)lpIndex; … … 818 820 /////////////////////////////////////// 819 821 820 if( type == DEF_OBJECT ){821 //オブジェクト の場合はありえない822 if( type == DEF_OBJECT || type == DEF_STRUCT ){ 823 //オブジェクトまたは構造体の場合はありえない 822 824 SetError(300,NULL,cp); 823 825 return 0; … … 855 857 *(_int64 *)(initGlobalBuf+offset)=i64data; 856 858 else if(type==DEF_LONG||type==DEF_DWORD||IsPtrType(type)){ 857 if(type==DEF_PTR_ BYTE&&lpCalcIndex==LITERAL_STRING){859 if(type==DEF_PTR_CHAR&&lpCalcIndex==LITERAL_STRING){ 858 860 //文字列定数のとき 859 861 … … 918 920 } 919 921 920 if(type==DEF_ OBJECT){922 if(type==DEF_STRUCT){ 921 923 CClass *pobj_c; 922 924 pobj_c=(CClass *)lpIndex; … … 1045 1047 } 1046 1048 else if(type==DEF_LONG||type==DEF_DWORD||IsPtrType(type)){ 1047 if(type==DEF_PTR_ BYTE&&lpCalcIndex==LITERAL_STRING){1049 if(type==DEF_PTR_CHAR&&lpCalcIndex==LITERAL_STRING){ 1048 1050 //文字列定数のとき 1049 1051 … … 1102 1104 } 1103 1105 1106 void dim(bool isRef, char *VarName,int *SubScripts,TYPEINFO &TypeInfo,int TypeSize,char *InitBuf,char *ConstractParameter,DWORD dwFlags){ 1107 extern BOOL bCompilingGlobal; 1108 if(bCompilingGlobal){ 1109 ///////////////////////// 1110 // グローバル変数 1111 ///////////////////////// 1112 1113 AddGlobalVariable(isRef,VarName,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags); 1114 } 1115 else{ 1116 ///////////////// 1117 // ローカル変数 1118 ///////////////// 1119 1120 int i2,i3; 1121 1122 for(i2=0;i2<MaxLocalVarNum;i2++){ 1123 if(LocalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==LocalVar[i2].ScopeLevel){ 1124 if(lstrcmp(LocalVar[i2].name,VarName)==0){ 1125 //2重定義のエラー 1126 SetError(15,VarName,cp); 1127 return; 1128 } 1129 } 1130 } 1131 1132 LocalVar=(VARIABLE *)HeapReAlloc(hHeap,0,LocalVar,(MaxLocalVarNum+1)*sizeof(VARIABLE)); 1133 VARIABLE *pVar = &LocalVar[MaxLocalVarNum]; 1134 MaxLocalVarNum++; 1135 1136 if( isRef ){ 1137 //参照型 1138 pVar->fRef = REF_VARIABLE; 1139 TypeSize = PTR_SIZE; 1140 } 1141 else pVar->fRef=0; 1142 1143 for(i2=1,i3=0;i3<255;i3++){ 1144 //配列要素数 1145 pVar->SubScripts[i3]=SubScripts[i3]; 1146 1147 if(SubScripts[i3]==-1) break; 1148 i2*=SubScripts[i3]+1; 1149 } 1150 int VarSize=TypeSize*i2; 1151 if(VarSize%4) VarSize+=4-(VarSize%4); 1152 1153 //変数データを追加 1154 lstrcpy(pVar->name,VarName); 1155 if(dwFlags & DIMFLAG_CONST) pVar->bConst = true; 1156 else pVar->bConst = false; 1157 if(SubScripts[0]==-1) pVar->bArray=0; 1158 else pVar->bArray=1; 1159 pVar->type=TypeInfo.type; 1160 pVar->u.index=TypeInfo.u.lpIndex; 1161 AllLocalVarSize+=VarSize; 1162 pVar->offset=AllLocalVarSize; 1163 1164 //レキシカルスコープ 1165 pVar->ScopeLevel=obj_LexScopes.GetNowLevel(); 1166 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress(); 1167 pVar->bLiving=TRUE; 1168 1169 if(InitBuf[0]){ 1170 //初期代入時のみ、書き込みアクセスを許可する 1171 bool bConstBack = pVar->bConst; 1172 pVar->bConst = false; 1173 1174 int result = 0; 1175 if( pVar->type != DEF_OBJECT ){ 1176 result = InitLocalVar(-pVar->offset, 1177 pVar->type, 1178 pVar->u.index, 1179 pVar->SubScripts, 1180 InitBuf); 1181 } 1182 1183 if(!result){ 1184 //動的な式だった場合は代入演算を行う 1185 char temporary[8192]; 1186 sprintf(temporary,"%s=%s",VarName,InitBuf); 1187 OpcodeCalc(temporary); 1188 } 1189 1190 pVar->bConst = bConstBack; 1191 } 1192 else{ 1193 //push 0 1194 op_push_value(0); 1195 1196 //push VarSize 1197 op_push_value(VarSize); 1198 1199 //mov eax,ebp 1200 OpBuffer[obp++]=(char)0x8B; 1201 OpBuffer[obp++]=(char)0xC5; 1202 1203 //add eax,offset 1204 OpBuffer[obp++]=(char)0x05; 1205 *((long *)(OpBuffer+obp))=-pVar->offset; 1206 AddLocalVarAddrSchedule(); 1207 obp+=sizeof(long); 1208 1209 //push eax 1210 op_push(REG_EAX); 1211 1212 //call FillMemory 1213 OpBuffer[obp++]=(char)0xFF; 1214 OpBuffer[obp++]=(char)0x15; 1215 DECLAREINFO *pdi; 1216 pdi=GetDeclareHash("FillMemory"); 1217 pdi->bUse=1; 1218 pobj_ImportAddrSchedule->add(pdi); 1219 obp+=sizeof(long); 1220 } 1221 } 1222 1223 //New呼び出し 1224 if(TypeInfo.type==DEF_OBJECT&&(dwFlags&DIMFLAG_NONCALL_CONSTRACTOR)==0&&InitBuf[0]=='\0'){ 1225 char objectSize[255]; 1226 if( SubScripts[0] == -1 ){ 1227 objectSize[0] = 0; 1228 } 1229 else{ 1230 if( SubScripts[1] != -1 ){ 1231 SetError(300,NULL,cp); 1232 } 1233 sprintf( objectSize, "%d", SubScripts[0] ); 1234 } 1235 Operator_New( *TypeInfo.u.pobj_Class, objectSize, ConstractParameter, TypeInfo ); 1236 1237 //pop eax 1238 op_pop( REG_EAX ); 1239 1240 int type; 1241 LONG_PTR lpIndex; 1242 RELATIVE_VAR RelativeVar; 1243 GetVarOffset( true, false, VarName, &type, &RelativeVar, &lpIndex ); 1244 if( RelativeVar.dwKind == VAR_DIRECTMEM ){ 1245 SetError(); 1246 } 1247 SetVariableFromEax( DEF_OBJECT, DEF_OBJECT, &RelativeVar ); 1248 } 1249 1250 if(TypeInfo.type==DEF_OBJECT){ 1251 if(TypeInfo.u.pobj_Class->IsAbstract()){ 1252 //抽象クラスだったとき 1253 SetError(125,TypeInfo.u.pobj_Class->name,cp); 1254 } 1255 } 1256 } 1104 1257 void dim(char *Parameter,DWORD dwFlags){ 1105 1258 extern BOOL bCompilingGlobal; 1106 1259 extern HANDLE hHeap; 1107 int i2 ,i3,VarSize;1260 int i2; 1108 1261 char VarName[VN_SIZE]; 1109 1262 … … 1187 1340 GetNowStaticVarFullName(VarName,temporary); 1188 1341 1189 AddGlobalVariable(isRef, temporary,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags);1342 dim( isRef,temporary,SubScripts,TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags ); 1190 1343 1191 1344 /* … … 1195 1348 } 1196 1349 else{ 1197 if(bCompilingGlobal){ 1198 ///////////////////////// 1199 // グローバル変数 1200 ///////////////////////// 1201 1202 AddGlobalVariable(isRef, VarName,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags); 1203 } 1204 else{ 1205 ///////////////// 1206 // ローカル変数 1207 ///////////////// 1208 1209 for(i2=0;i2<MaxLocalVarNum;i2++){ 1210 if(LocalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==LocalVar[i2].ScopeLevel){ 1211 if(lstrcmp(LocalVar[i2].name,VarName)==0){ 1212 //2重定義のエラー 1213 SetError(15,VarName,cp); 1214 return; 1215 } 1216 } 1217 } 1218 1219 LocalVar=(VARIABLE *)HeapReAlloc(hHeap,0,LocalVar,(MaxLocalVarNum+1)*sizeof(VARIABLE)); 1220 VARIABLE *pVar = &LocalVar[MaxLocalVarNum]; 1221 MaxLocalVarNum++; 1222 1223 if( isRef ){ 1224 //参照型 1225 pVar->fRef = REF_VARIABLE; 1226 TypeSize = PTR_SIZE; 1227 } 1228 else pVar->fRef=0; 1229 1230 for(i2=1,i3=0;i3<255;i3++){ 1231 //配列要素数 1232 pVar->SubScripts[i3]=SubScripts[i3]; 1233 1234 if(SubScripts[i3]==-1) break; 1235 i2*=SubScripts[i3]+1; 1236 } 1237 VarSize=TypeSize*i2; 1238 if(VarSize%4) VarSize+=4-(VarSize%4); 1239 1240 //変数データを追加 1241 lstrcpy(pVar->name,VarName); 1242 if(dwFlags & DIMFLAG_CONST) pVar->bConst = true; 1243 else pVar->bConst = false; 1244 if(SubScripts[0]==-1) pVar->bArray=0; 1245 else pVar->bArray=1; 1246 pVar->type=TypeInfo.type; 1247 pVar->u.index=TypeInfo.u.lpIndex; 1248 AllLocalVarSize+=VarSize; 1249 pVar->offset=AllLocalVarSize; 1250 1251 //レキシカルスコープ 1252 pVar->ScopeLevel=obj_LexScopes.GetNowLevel(); 1253 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress(); 1254 pVar->bLiving=TRUE; 1255 1256 if(InitBuf[0]){ 1257 //初期代入時のみ、書き込みアクセスを許可する 1258 bool bConstBack = pVar->bConst; 1259 pVar->bConst = false; 1260 1261 int result = InitLocalVar(-pVar->offset, 1262 pVar->type, 1263 pVar->u.index, 1264 pVar->SubScripts, 1265 InitBuf); 1266 1267 if(!result){ 1268 if( isRef ){ 1269 SetRefVariable( VarName, InitBuf ); 1270 } 1271 else{ 1272 char temporary[8192]; 1273 sprintf(temporary,"%s=%s",VarName,InitBuf); 1274 OpcodeCalc(temporary); 1275 } 1276 } 1277 1278 pVar->bConst = bConstBack; 1279 } 1280 else{ 1281 //push 0 1282 op_push_value(0); 1283 1284 //push VarSize 1285 op_push_value(VarSize); 1286 1287 //mov eax,ebp 1288 OpBuffer[obp++]=(char)0x8B; 1289 OpBuffer[obp++]=(char)0xC5; 1290 1291 //add eax,offset 1292 OpBuffer[obp++]=(char)0x05; 1293 *((long *)(OpBuffer+obp))=-pVar->offset; 1294 AddLocalVarAddrSchedule(); 1295 obp+=sizeof(long); 1296 1297 //push eax 1298 op_push(REG_EAX); 1299 1300 //call FillMemory 1301 OpBuffer[obp++]=(char)0xFF; 1302 OpBuffer[obp++]=(char)0x15; 1303 DECLAREINFO *pdi; 1304 pdi=GetDeclareHash("FillMemory"); 1305 pdi->bUse=1; 1306 pobj_ImportAddrSchedule->add(pdi); 1307 obp+=sizeof(long); 1308 } 1309 } 1310 1311 //コンストラクタ呼び出し 1312 if(TypeInfo.type==DEF_OBJECT&&(dwFlags&DIMFLAG_NONCALL_CONSTRACTOR)==0&& isRef == false ){ 1313 CallConstructor(VarName,SubScripts,TypeInfo,ConstractParameter); 1314 } 1315 } 1316 1317 if(TypeInfo.type==DEF_OBJECT){ 1318 if(TypeInfo.u.pobj_Class->IsAbstract()){ 1319 //抽象クラスだったとき 1320 SetError(125,TypeInfo.u.pobj_Class->name,cp); 1321 } 1350 dim( isRef,VarName,SubScripts,TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags ); 1322 1351 } 1323 1352 }
Note:
See TracChangeset
for help on using the changeset viewer.