Changeset 64 in dev for BasicCompiler64/Compile_Var.cpp
- Timestamp:
- Mar 8, 2007, 2:49:34 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
BasicCompiler64/Compile_Var.cpp
r63 r64 41 41 42 42 if(PTR_LEVEL(*pType)){ 43 *pType=MAKE_PTR_TYPE(NATURAL_TYPE(*pType),PTR_LEVEL(*pType)-1); 44 if((*pType)==DEF_OBJECT){ 45 CClass *pClass = (CClass *)lpIndex; 46 //imul reg,objsize 47 op_imul_value( sizeof(_int64), reg, pClass->GetSize() ); 48 } 49 else{ 50 i2=GetTypeSize(*pType,-1); 51 if(i2>=2){ 52 //imul reg,i2 53 op_imul_value(sizeof(_int64),reg,i2); 54 } 43 *pType = PTR_LEVEL_DOWN( *pType ); 44 45 i2=GetTypeSize(*pType,-1); 46 if(i2>=2){ 47 //imul reg,i2 48 op_imul_RV(sizeof(_int64),reg,i2); 55 49 } 56 50 } … … 71 65 //add r11,reg 72 66 op_add64_reg(REG_R11,reg); 67 } 68 void SetRelativeOffset( RELATIVE_VAR &relativeVar ){ 69 if(relativeVar.dwKind==VAR_DIRECTMEM){ 70 //mov r11,qword ptr[r11] 71 op_mov_RM(sizeof(_int64),REG_R11,REG_R11,0,MOD_BASE); 72 } 73 else{ 74 //直接参照に切り替え 75 SetVarPtrToReg(REG_R12,&relativeVar); 76 relativeVar.dwKind=VAR_DIRECTMEM; 77 78 //mov r11,qword ptr[r12] 79 op_mov_RM(sizeof(_int64),REG_R11,REG_R12,0,MOD_BASE); 80 } 73 81 } 74 82 BOOL GetArrayOffset(int *SubScripts,char *array,int type,LONG_PTR lpIndex){ … … 153 161 154 162 //imul reg,i4 155 op_imul_ value(sizeof(_int64),reg,i4);163 op_imul_RV(sizeof(_int64),reg,i4); 156 164 157 165 //add r12,reg … … 192 200 char lpPtrOffset[VN_SIZE]; //第2次配列 193 201 char NestMember[VN_SIZE]; //入れ子メンバ 194 int RefType; //"."参照のときは0、"->"参照のときは1202 CClass::RefType refType; 195 203 lstrcpy(VarName,member); 196 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember, &RefType)) return 0;204 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember,refType)) return 0; 197 205 198 206 … … 279 287 //入れ子構造の場合 280 288 281 if(*pType==DEF_OBJECT && pMember->IsRef() == false){282 if( RefType!=DEF_OBJECT){289 if(*pType==DEF_OBJECT || *pType==DEF_STRUCT){ 290 if( refType != CClass::Dot ){ 283 291 if(isErrorEnabled) SetError(104,member,cp); 284 292 return 0; 285 293 } 286 } 287 else if(*pType==DEF_PTR_OBJECT || ( *pType==DEF_OBJECT && pMember->IsRef() )){ 294 295 if( *pType==DEF_OBJECT ){ 296 // 参照内容へのポインタを抽出 297 SetRelativeOffset( *pRelativeVar ); 298 } 299 } 300 else if(*pType==DEF_PTR_OBJECT||*pType==DEF_PTR_STRUCT){ 288 301 //構造体ポインタ型メンバ変数 289 302 290 303 if(lpPtrOffset[0]){ 291 304 //pObj[n].member 292 if( *pType==DEF_PTR_OBJECT&&RefType!=DEF_OBJECT){305 if( ( *pType==DEF_PTR_OBJECT||*pType==DEF_PTR_STRUCT )&& refType != CClass::Dot ){ 293 306 if(isErrorEnabled) SetError(104,member,cp); 294 307 return 0; … … 303 316 else{ 304 317 //pObj->member 305 if( *pType==DEF_PTR_OBJECT&&RefType!=DEF_PTR_OBJECT){318 if( ( *pType==DEF_PTR_OBJECT||*pType==DEF_PTR_STRUCT )&& refType != CClass::Pointer ){ 306 319 if(isErrorEnabled) SetError(104,member,cp); 307 320 return 0; 308 321 } 309 322 310 if(pRelativeVar->dwKind==VAR_DIRECTMEM){ 311 //mov r11,qword ptr[r11] 312 op_mov_RM(sizeof(_int64),REG_R11,REG_R11,0,MOD_BASE); 313 } 314 else{ 315 //直接参照に切り替え 316 SetVarPtrToReg(REG_R12,pRelativeVar); 317 pRelativeVar->dwKind=VAR_DIRECTMEM; 318 319 //mov r11,qword ptr[r12] 320 op_mov_RM(sizeof(_int64),REG_R11,REG_R12,0,MOD_BASE); 321 } 322 } 323 } 324 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2)){ 323 SetRelativeOffset( *pRelativeVar ); 324 } 325 } 326 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2) || *pType==MAKE_PTR_TYPE(DEF_STRUCT,2)){ 325 327 //構造体ポインタのポインタ型メンバ変数 326 328 327 329 if(lpPtrOffset[0]){ 328 330 //ppObj[n]->member 329 if( RefType!=DEF_PTR_OBJECT){331 if( refType != CClass::Pointer ){ 330 332 if(isErrorEnabled) SetError(104,member,cp); 331 333 return 0; … … 388 390 BOOL GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss){ 389 391 extern BOOL bCompilingGlobal; 390 int i ,RefType;392 int i; 391 393 LONG_PTR lpIndex; 392 394 char variable[VN_SIZE],member[VN_SIZE],VarName[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE]; … … 398 400 else lstrcpy(variable,NameBuffer); 399 401 400 if( FormatUseProcReturnObject( variable, VarName, array, RefType, member ) ){ 402 CClass::RefType refType; 403 if( FormatUseProcReturnObject( variable, VarName, array, refType, member ) ){ 401 404 // 戻り値オブジェクトのメンバを直接参照しているとき 402 405 //例: func().member … … 442 445 443 446 lstrcpy(VarName,variable); 444 GetVarFormatString(VarName,array,lpPtrOffset,member, &RefType);447 GetVarFormatString(VarName,array,lpPtrOffset,member,refType); 445 448 446 449 int *pSubScripts; … … 581 584 char tempMember[VN_SIZE]; 582 585 char tempArray[VN_SIZE]; 583 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember,&i); 586 { 587 CClass::RefType refType; 588 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember, refType ); 589 } 584 590 585 591 char temp2[VN_SIZE]; … … 697 703 } 698 704 if(member[0]){ 699 if(*pType==DEF_OBJECT ){705 if(*pType==DEF_OBJECT || *pType==DEF_STRUCT){ 700 706 //実態オブジェクトのメンバを参照(obj.member) 701 if( RefType!=DEF_OBJECT){707 if( refType != CClass::Dot ){ 702 708 SetError(104,VarName,cp); 703 709 pRelativeVar->dwKind=NON_VAR; 704 710 return 0; 705 711 } 706 } 707 else if(*pType==DEF_PTR_OBJECT){ 712 713 if( *pType==DEF_OBJECT ){ 714 // 参照内容へのポインタを抽出 715 SetRelativeOffset( *pRelativeVar ); 716 } 717 } 718 else if(*pType==DEF_PTR_OBJECT || *pType==DEF_PTR_STRUCT){ 708 719 //ポインタオブジェクトが示すメンバを参照 709 720 if(lpPtrOffset[0]){ 710 721 //pObj[n].member 711 if( RefType!=DEF_OBJECT){722 if( refType != CClass::Dot ){ 712 723 SetError(104,VarName,cp); 713 724 pRelativeVar->dwKind=NON_VAR; … … 719 730 else{ 720 731 //pObj->member 721 if( RefType!=DEF_PTR_OBJECT){732 if( refType != CClass::Pointer ){ 722 733 SetError(104,VarName,cp); 723 734 pRelativeVar->dwKind=NON_VAR; … … 732 743 } 733 744 } 734 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2) ){745 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2) || *pType==MAKE_PTR_TYPE(DEF_STRUCT,2)){ 735 746 //ポインタオブジェクトが示すメンバを参照 736 747 if(lpPtrOffset[0]){ 737 748 //ppObj[n]->member 738 if( RefType!=DEF_PTR_OBJECT){749 if( refType != CClass::Pointer ){ 739 750 SetError(104,VarName,cp); 740 751 pRelativeVar->dwKind=NON_VAR; … … 815 826 } 816 827 817 if(type==DEF_ OBJECT){828 if(type==DEF_STRUCT){ 818 829 CClass *pobj_c; 819 830 pobj_c=(CClass *)lpIndex; … … 848 859 /////////////////////////////////////// 849 860 850 if( type == DEF_OBJECT ){851 //オブジェクト の場合はありえない861 if( type == DEF_OBJECT || type == DEF_STRUCT ){ 862 //オブジェクトまたは構造体の場合はありえない 852 863 SetError(300,NULL,cp); 853 864 return 0; … … 888 899 *(float *)(initGlobalBuf+offset)=(float)dbl; 889 900 else if(type==DEF_INT64||type==DEF_QWORD||IsPtrType(type)){ 890 if(type==DEF_PTR_ BYTE&&lpCalcIndex==LITERAL_STRING){901 if(type==DEF_PTR_CHAR&&lpCalcIndex==LITERAL_STRING){ 891 902 //文字列定数のとき 892 903 … … 953 964 } 954 965 955 if(type==DEF_ OBJECT){966 if(type==DEF_STRUCT){ 956 967 CClass *pobj_c; 957 968 pobj_c=(CClass *)lpIndex; … … 1037 1048 } 1038 1049 else if(type==DEF_INT64||type==DEF_QWORD||IsPtrType(type)){ 1039 if(type==DEF_PTR_ BYTE&&lpCalcIndex==LITERAL_STRING){1050 if(type==DEF_PTR_CHAR&&lpCalcIndex==LITERAL_STRING){ 1040 1051 //文字列定数のとき 1041 1052 … … 1101 1112 } 1102 1113 1103 1114 void dim(bool isRef, char *VarName,int *SubScripts,TYPEINFO &TypeInfo,int TypeSize,char *InitBuf,char *ConstractParameter,DWORD dwFlags){ 1115 extern BOOL bCompilingGlobal; 1116 if(bCompilingGlobal){ 1117 ///////////////////////// 1118 // グローバル変数 1119 ///////////////////////// 1120 1121 AddGlobalVariable(isRef,VarName,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags); 1122 } 1123 else{ 1124 ///////////////// 1125 // ローカル変数 1126 ///////////////// 1127 1128 int i2,i3; 1129 1130 for(i2=0;i2<MaxLocalVarNum;i2++){ 1131 if(LocalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==LocalVar[i2].ScopeLevel){ 1132 if(lstrcmp(LocalVar[i2].name,VarName)==0){ 1133 //2重定義のエラー 1134 SetError(15,VarName,cp); 1135 return; 1136 } 1137 } 1138 } 1139 1140 LocalVar=(VARIABLE *)HeapReAlloc(hHeap,0,LocalVar,(MaxLocalVarNum+1)*sizeof(VARIABLE)); 1141 VARIABLE *pVar = &LocalVar[MaxLocalVarNum]; 1142 MaxLocalVarNum++; 1143 1144 if( isRef ){ 1145 //参照型 1146 pVar->fRef = REF_VARIABLE; 1147 TypeSize = PTR_SIZE; 1148 } 1149 else pVar->fRef=0; 1150 1151 for(i2=1,i3=0;i3<255;i3++){ 1152 //配列要素数 1153 pVar->SubScripts[i3]=SubScripts[i3]; 1154 1155 if(SubScripts[i3]==-1) break; 1156 i2*=SubScripts[i3]+1; 1157 } 1158 int VarSize=TypeSize*i2; 1159 if(VarSize%8) VarSize+=8-(VarSize%8); 1160 1161 //変数データを追加 1162 lstrcpy(pVar->name,VarName); 1163 if(dwFlags & DIMFLAG_CONST) pVar->bConst = true; 1164 else pVar->bConst = false; 1165 if(SubScripts[0]==-1) pVar->bArray=0; 1166 else pVar->bArray=1; 1167 pVar->type=TypeInfo.type; 1168 pVar->u.index=TypeInfo.u.lpIndex; 1169 1170 //アラインメントを考慮 1171 if( pVar->type==DEF_STRUCT ){ 1172 int alignment = pVar->u.pobj_c->iAlign; 1173 if( alignment ){ 1174 if( AllLocalVarSize % alignment ){ 1175 AllLocalVarSize += alignment - (AllLocalVarSize % alignment); 1176 } 1177 } 1178 } 1179 1180 AllLocalVarSize+=VarSize; 1181 pVar->offset=AllLocalVarSize; 1182 1183 //レキシカルスコープ 1184 pVar->ScopeLevel=obj_LexScopes.GetNowLevel(); 1185 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress(); 1186 pVar->bLiving=TRUE; 1187 1188 if(InitBuf[0]){ 1189 //初期代入時のみ、書き込みアクセスを許可する 1190 bool bConstBack = pVar->bConst; 1191 pVar->bConst = false; 1192 1193 int result = 0; 1194 if( pVar->type != DEF_OBJECT ){ 1195 result = InitLocalVar(-pVar->offset, 1196 pVar->type, 1197 pVar->u.index, 1198 pVar->SubScripts, 1199 InitBuf); 1200 } 1201 1202 if(!result){ 1203 //動的な式だった場合は代入演算を行う 1204 char temporary[8192]; 1205 sprintf(temporary,"%s=%s",VarName,InitBuf); 1206 OpcodeCalc(temporary); 1207 } 1208 1209 pVar->bConst = bConstBack; 1210 } 1211 else{ 1212 //0初期化 1213 1214 //mov r8, 0 1215 op_zero_reg( REG_R8 ); 1216 1217 //mov rdx, VarSize 1218 op_mov_RV( sizeof(_int64), REG_RDX, VarSize ); 1219 1220 //mov rcx, rsp 1221 op_mov_RR( REG_RCX, REG_RSP ); 1222 1223 //add rcx, offset 1224 op_add_RV( REG_RCX, -pVar->offset ); 1225 obp-=sizeof(long); 1226 AddLocalVarAddrSchedule(); 1227 obp+=sizeof(long); 1228 1229 //call FillMemory 1230 DECLAREINFO *pdi; 1231 pdi=GetDeclareHash("FillMemory"); 1232 op_call( pdi ); 1233 } 1234 } 1235 1236 //コンストラクタ呼び出し 1237 if(TypeInfo.type==DEF_OBJECT&&(dwFlags&DIMFLAG_NONCALL_CONSTRACTOR)==0&&InitBuf[0]=='\0'){ 1238 char objectSize[255]; 1239 if( SubScripts[0] == -1 ){ 1240 objectSize[0] = 0; 1241 } 1242 else{ 1243 if( SubScripts[1] != -1 ){ 1244 SetError(300,NULL,cp); 1245 } 1246 sprintf( objectSize, "%d", SubScripts[0] ); 1247 } 1248 Operator_New( *TypeInfo.u.pobj_Class, objectSize, ConstractParameter, TypeInfo ); 1249 1250 int type; 1251 LONG_PTR lpIndex; 1252 RELATIVE_VAR RelativeVar; 1253 GetVarOffset( true, false, VarName, &type, &RelativeVar, &lpIndex ); 1254 if( RelativeVar.dwKind == VAR_DIRECTMEM ){ 1255 SetError(); 1256 } 1257 SetVariableFromRax( DEF_OBJECT, DEF_OBJECT, &RelativeVar ); 1258 } 1259 1260 if(TypeInfo.type==DEF_OBJECT){ 1261 if(TypeInfo.u.pobj_Class->IsAbstract()){ 1262 //抽象クラスだったとき 1263 SetError(125,TypeInfo.u.pobj_Class->name,cp); 1264 } 1265 } 1266 } 1104 1267 void dim(char *Parameter,DWORD dwFlags){ 1105 1268 extern BOOL bCompilingGlobal; 1106 1269 extern HANDLE hHeap; 1107 int i2 ,i3,VarSize;1270 int i2; 1108 1271 char VarName[VN_SIZE]; 1109 1272 … … 1172 1335 TypeSize=GetTypeSize(TypeInfo.type,TypeInfo.u.lpIndex); 1173 1336 1337 extern BOOL bCompilingGlobal; 1174 1338 if(dwFlags&DIMFLAG_STATIC){ 1175 1339 if(bCompilingGlobal){ … … 1186 1350 GetNowStaticVarFullName(VarName,temporary); 1187 1351 1188 AddGlobalVariable(isRef,temporary,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags);1352 dim( isRef,temporary,SubScripts,TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags ); 1189 1353 1190 1354 /* … … 1194 1358 } 1195 1359 else{ 1196 if(bCompilingGlobal){ 1197 ///////////////////////// 1198 // グローバル変数 1199 ///////////////////////// 1200 1201 AddGlobalVariable(isRef,VarName,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags); 1202 } 1203 else{ 1204 ///////////////// 1205 // ローカル変数 1206 ///////////////// 1207 1208 for(i2=0;i2<MaxLocalVarNum;i2++){ 1209 if(LocalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==LocalVar[i2].ScopeLevel){ 1210 if(lstrcmp(LocalVar[i2].name,VarName)==0){ 1211 //2重定義のエラー 1212 SetError(15,VarName,cp); 1213 return; 1214 } 1215 } 1216 } 1217 1218 LocalVar=(VARIABLE *)HeapReAlloc(hHeap,0,LocalVar,(MaxLocalVarNum+1)*sizeof(VARIABLE)); 1219 VARIABLE *pVar = &LocalVar[MaxLocalVarNum]; 1220 MaxLocalVarNum++; 1221 1222 if( isRef ){ 1223 //参照型 1224 pVar->fRef = REF_VARIABLE; 1225 TypeSize = PTR_SIZE; 1226 } 1227 else pVar->fRef=0; 1228 1229 for(i2=1,i3=0;i3<255;i3++){ 1230 //配列要素数 1231 pVar->SubScripts[i3]=SubScripts[i3]; 1232 1233 if(SubScripts[i3]==-1) break; 1234 i2*=SubScripts[i3]+1; 1235 } 1236 VarSize=TypeSize*i2; 1237 if(VarSize%8) VarSize+=8-(VarSize%8); 1238 1239 //変数データを追加 1240 lstrcpy(pVar->name,VarName); 1241 if(dwFlags & DIMFLAG_CONST) pVar->bConst = true; 1242 else pVar->bConst = false; 1243 if(SubScripts[0]==-1) pVar->bArray=0; 1244 else pVar->bArray=1; 1245 pVar->type=TypeInfo.type; 1246 pVar->u.index=TypeInfo.u.lpIndex; 1247 AllLocalVarSize+=VarSize; 1248 pVar->offset=AllLocalVarSize; 1249 1250 //レキシカルスコープ 1251 pVar->ScopeLevel=obj_LexScopes.GetNowLevel(); 1252 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress(); 1253 pVar->bLiving=TRUE; 1254 1255 if(InitBuf[0]){ 1256 //初期代入時のみ、書き込みアクセスを許可する 1257 bool bConstBack = pVar->bConst; 1258 pVar->bConst = false; 1259 1260 int result = InitLocalVar(-pVar->offset, 1261 pVar->type, 1262 pVar->u.index, 1263 pVar->SubScripts, 1264 InitBuf); 1265 1266 if(!result){ 1267 //動的な式だった場合は代入演算を行う 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 //0初期化 1282 1283 //mov r8, 0 1284 op_zero_reg( REG_R8 ); 1285 1286 //mov rdx, VarSize 1287 op_mov_RV( sizeof(_int64), REG_RDX, VarSize ); 1288 1289 //mov rcx, rsp 1290 op_mov_RR( REG_RCX, REG_RSP ); 1291 1292 //add rcx, offset 1293 op_add64_value( REG_RCX, -pVar->offset ); 1294 obp-=sizeof(long); 1295 AddLocalVarAddrSchedule(); 1296 obp+=sizeof(long); 1297 1298 //call FillMemory 1299 DECLAREINFO *pdi; 1300 pdi=GetDeclareHash("FillMemory"); 1301 op_call( pdi ); 1302 } 1303 } 1304 1305 //コンストラクタ呼び出し 1306 if(TypeInfo.type==DEF_OBJECT&&(dwFlags&DIMFLAG_NONCALL_CONSTRACTOR)==0&& isRef == false ){ 1307 CallConstructor(VarName,SubScripts,TypeInfo,ConstractParameter); 1308 } 1309 } 1310 1311 if(TypeInfo.type==DEF_OBJECT){ 1312 if(TypeInfo.u.pobj_Class->IsAbstract()){ 1313 //抽象クラスだったとき 1314 SetError(125,TypeInfo.u.pobj_Class->name,cp); 1315 } 1360 dim( isRef,VarName,SubScripts,TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlags ); 1316 1361 } 1317 1362 } … … 1362 1407 1363 1408 //mov reg,r11 1364 op_mov 64_ToReg_FromReg(reg,REG_R11);1409 op_mov_RR(reg,REG_R11); 1365 1410 } 1366 1411 else{ … … 1404 1449 1405 1450 //mov reg,r11 1406 op_mov 64_ToReg_FromReg(reg,REG_R11);1451 op_mov_RR(reg,REG_R11); 1407 1452 } 1408 1453 else{ 1409 1454 //mov reg,rsp 1410 op_mov 64_ToReg_FromReg(reg,REG_RSP);1455 op_mov_RR(reg,REG_RSP); 1411 1456 1412 1457 //add reg,offset 1413 op_add 64_value(reg,(int)pRelativeVar->offset);1458 op_add_RV(reg,(int)pRelativeVar->offset); 1414 1459 obp-=sizeof(long); 1415 1460 AddLocalVarAddrSchedule(); … … 1441 1486 directmem: 1442 1487 //mov reg,r11 1443 op_mov 64_ToReg_FromReg(reg,REG_R11);1488 op_mov_RR(reg,REG_R11); 1444 1489 } 1445 1490 }
Note:
See TracChangeset
for help on using the changeset viewer.