Mercurial > projects > ldc
comparison gen/statements.cpp @ 1145:40caa8207b3e
Moved IRTargetScopeS from IRState into IrFunction, fixes #240 .
author | Tomas Lindquist Olsen <tomas.l.olsen gmail.com> |
---|---|
date | Thu, 26 Mar 2009 18:46:21 +0100 |
parents | f99a3b393c03 |
children | 7d28dcbff23e |
comparison
equal
deleted
inserted
replaced
1144:cdda73cf2f8a | 1145:40caa8207b3e |
---|---|
294 | 294 |
295 // rewrite scope | 295 // rewrite scope |
296 gIR->scope() = IRScope(whilebodybb,endbb); | 296 gIR->scope() = IRScope(whilebodybb,endbb); |
297 | 297 |
298 // while body code | 298 // while body code |
299 p->targetScopes.push_back(IRTargetScope(this,NULL,whilebb,endbb)); | 299 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,whilebb,endbb)); |
300 body->toIR(p); | 300 body->toIR(p); |
301 p->targetScopes.pop_back(); | 301 p->func()->targetScopes.pop_back(); |
302 | 302 |
303 // loop | 303 // loop |
304 if (!gIR->scopereturned()) | 304 if (!gIR->scopereturned()) |
305 llvm::BranchInst::Create(whilebb, gIR->scopebb()); | 305 llvm::BranchInst::Create(whilebb, gIR->scopebb()); |
306 | 306 |
330 | 330 |
331 // replace current scope | 331 // replace current scope |
332 gIR->scope() = IRScope(dowhilebb,condbb); | 332 gIR->scope() = IRScope(dowhilebb,condbb); |
333 | 333 |
334 // do-while body code | 334 // do-while body code |
335 p->targetScopes.push_back(IRTargetScope(this,NULL,condbb,endbb)); | 335 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,condbb,endbb)); |
336 body->toIR(p); | 336 body->toIR(p); |
337 p->targetScopes.pop_back(); | 337 p->func()->targetScopes.pop_back(); |
338 | 338 |
339 // branch to condition block | 339 // branch to condition block |
340 llvm::BranchInst::Create(condbb, gIR->scopebb()); | 340 llvm::BranchInst::Create(condbb, gIR->scopebb()); |
341 gIR->scope() = IRScope(condbb,endbb); | 341 gIR->scope() = IRScope(condbb,endbb); |
342 | 342 |
375 | 375 |
376 // move into the for condition block, ie. start the loop | 376 // move into the for condition block, ie. start the loop |
377 assert(!gIR->scopereturned()); | 377 assert(!gIR->scopereturned()); |
378 llvm::BranchInst::Create(forbb, gIR->scopebb()); | 378 llvm::BranchInst::Create(forbb, gIR->scopebb()); |
379 | 379 |
380 p->targetScopes.push_back(IRTargetScope(this,NULL,forincbb,endbb)); | 380 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,forincbb,endbb)); |
381 | 381 |
382 // replace current scope | 382 // replace current scope |
383 gIR->scope() = IRScope(forbb,forbodybb); | 383 gIR->scope() = IRScope(forbb,forbodybb); |
384 | 384 |
385 // create the condition | 385 // create the condition |
418 | 418 |
419 // loop | 419 // loop |
420 if (!gIR->scopereturned()) | 420 if (!gIR->scopereturned()) |
421 llvm::BranchInst::Create(forbb, gIR->scopebb()); | 421 llvm::BranchInst::Create(forbb, gIR->scopebb()); |
422 | 422 |
423 p->targetScopes.pop_back(); | 423 p->func()->targetScopes.pop_back(); |
424 | 424 |
425 // rewrite the scope | 425 // rewrite the scope |
426 gIR->scope() = IRScope(endbb,oldend); | 426 gIR->scope() = IRScope(endbb,oldend); |
427 } | 427 } |
428 | 428 |
452 while(tmp = targetLoopStatement->isScopeStatement()) | 452 while(tmp = targetLoopStatement->isScopeStatement()) |
453 targetLoopStatement = tmp->statement; | 453 targetLoopStatement = tmp->statement; |
454 | 454 |
455 // find the right break block and jump there | 455 // find the right break block and jump there |
456 bool found = false; | 456 bool found = false; |
457 IRState::TargetScopeVec::reverse_iterator it; | 457 IrFunction::TargetScopeVec::reverse_iterator it = p->func()->targetScopes.rbegin(); |
458 for(it = p->targetScopes.rbegin(); it != p->targetScopes.rend(); ++it) { | 458 IrFunction::TargetScopeVec::reverse_iterator it_end = p->func()->targetScopes.rend(); |
459 while(it != it_end) { | |
459 if(it->s == targetLoopStatement) { | 460 if(it->s == targetLoopStatement) { |
460 llvm::BranchInst::Create(it->breakTarget, p->scopebb()); | 461 llvm::BranchInst::Create(it->breakTarget, p->scopebb()); |
461 found = true; | 462 found = true; |
462 break; | 463 break; |
463 } | 464 } |
465 ++it; | |
464 } | 466 } |
465 assert(found); | 467 assert(found); |
466 } | 468 } |
467 else { | 469 else { |
468 // find closest scope with a break target | 470 // find closest scope with a break target |
469 IRState::TargetScopeVec::reverse_iterator it; | 471 IrFunction::TargetScopeVec::reverse_iterator it = p->func()->targetScopes.rbegin(); |
470 for(it = gIR->targetScopes.rbegin(); it != gIR->targetScopes.rend(); ++it) { | 472 IrFunction::TargetScopeVec::reverse_iterator it_end = p->func()->targetScopes.rend(); |
473 while(it != it_end) { | |
471 if(it->breakTarget) { | 474 if(it->breakTarget) { |
472 break; | 475 break; |
473 } | 476 } |
477 ++it; | |
474 } | 478 } |
475 DtoEnclosingHandlers(loc, it->s); | 479 DtoEnclosingHandlers(loc, it->s); |
476 llvm::BranchInst::Create(it->breakTarget, gIR->scopebb()); | 480 llvm::BranchInst::Create(it->breakTarget, gIR->scopebb()); |
477 } | 481 } |
478 | 482 |
503 while(tmp = targetLoopStatement->isScopeStatement()) | 507 while(tmp = targetLoopStatement->isScopeStatement()) |
504 targetLoopStatement = tmp->statement; | 508 targetLoopStatement = tmp->statement; |
505 | 509 |
506 // find the right continue block and jump there | 510 // find the right continue block and jump there |
507 bool found = false; | 511 bool found = false; |
508 IRState::TargetScopeVec::reverse_iterator it; | 512 IrFunction::TargetScopeVec::reverse_iterator it = p->func()->targetScopes.rbegin(); |
509 for(it = gIR->targetScopes.rbegin(); it != gIR->targetScopes.rend(); ++it) { | 513 IrFunction::TargetScopeVec::reverse_iterator it_end = p->func()->targetScopes.rend(); |
514 while(it != it_end) { | |
510 if(it->s == targetLoopStatement) { | 515 if(it->s == targetLoopStatement) { |
511 llvm::BranchInst::Create(it->continueTarget, gIR->scopebb()); | 516 llvm::BranchInst::Create(it->continueTarget, gIR->scopebb()); |
512 found = true; | 517 found = true; |
513 break; | 518 break; |
514 } | 519 } |
520 ++it; | |
515 } | 521 } |
516 assert(found); | 522 assert(found); |
517 } | 523 } |
518 else { | 524 else { |
519 // find closest scope with a continue target | 525 // find closest scope with a continue target |
520 IRState::TargetScopeVec::reverse_iterator it; | 526 IrFunction::TargetScopeVec::reverse_iterator it = p->func()->targetScopes.rbegin(); |
521 for(it = gIR->targetScopes.rbegin(); it != gIR->targetScopes.rend(); ++it) { | 527 IrFunction::TargetScopeVec::reverse_iterator it_end = p->func()->targetScopes.rend(); |
528 while(it != it_end) { | |
522 if(it->continueTarget) { | 529 if(it->continueTarget) { |
523 break; | 530 break; |
524 } | 531 } |
532 ++it; | |
525 } | 533 } |
526 DtoEnclosingHandlers(loc, it->s); | 534 DtoEnclosingHandlers(loc, it->s); |
527 llvm::BranchInst::Create(it->continueTarget, gIR->scopebb()); | 535 llvm::BranchInst::Create(it->continueTarget, gIR->scopebb()); |
528 } | 536 } |
529 | 537 |
591 // do the try block | 599 // do the try block |
592 // | 600 // |
593 p->scope() = IRScope(trybb,finallybb); | 601 p->scope() = IRScope(trybb,finallybb); |
594 | 602 |
595 assert(body); | 603 assert(body); |
596 p->targetScopes.push_back(IRTargetScope(this,new EnclosingTryFinally(this),NULL,NULL)); | 604 p->func()->targetScopes.push_back(IRTargetScope(this,new EnclosingTryFinally(this),NULL,NULL)); |
597 body->toIR(p); | 605 body->toIR(p); |
598 p->targetScopes.pop_back(); | 606 p->func()->targetScopes.pop_back(); |
599 | 607 |
600 // terminate try BB | 608 // terminate try BB |
601 if (!p->scopereturned()) | 609 if (!p->scopereturned()) |
602 llvm::BranchInst::Create(finallybb, p->scopebb()); | 610 llvm::BranchInst::Create(finallybb, p->scopebb()); |
603 | 611 |
849 | 857 |
850 // do switch body | 858 // do switch body |
851 assert(body); | 859 assert(body); |
852 | 860 |
853 p->scope() = IRScope(bodybb, endbb); | 861 p->scope() = IRScope(bodybb, endbb); |
854 p->targetScopes.push_back(IRTargetScope(this,NULL,NULL,endbb)); | 862 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,NULL,endbb)); |
855 body->toIR(p); | 863 body->toIR(p); |
856 p->targetScopes.pop_back(); | 864 p->func()->targetScopes.pop_back(); |
857 | 865 |
858 if (!p->scopereturned()) | 866 if (!p->scopereturned()) |
859 llvm::BranchInst::Create(endbb, p->scopebb()); | 867 llvm::BranchInst::Create(endbb, p->scopebb()); |
860 | 868 |
861 // add the cases | 869 // add the cases |
970 // update scope | 978 // update scope |
971 p->scope() = IRScope(thisbb,nextbb); | 979 p->scope() = IRScope(thisbb,nextbb); |
972 | 980 |
973 // push loop scope | 981 // push loop scope |
974 // continue goes to next statement, break goes to end | 982 // continue goes to next statement, break goes to end |
975 p->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); | 983 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); |
976 | 984 |
977 // do statement | 985 // do statement |
978 s->toIR(p); | 986 s->toIR(p); |
979 | 987 |
980 // pop loop scope | 988 // pop loop scope |
981 p->targetScopes.pop_back(); | 989 p->func()->targetScopes.pop_back(); |
982 | 990 |
983 // next stmt | 991 // next stmt |
984 if (!p->scopereturned()) | 992 if (!p->scopereturned()) |
985 p->ir->CreateBr(nextbb); | 993 p->ir->CreateBr(nextbb); |
986 } | 994 } |
1093 DtoAssign(loc, &dst, &src); | 1101 DtoAssign(loc, &dst, &src); |
1094 value->ir.irLocal->value = valvar; | 1102 value->ir.irLocal->value = valvar; |
1095 } | 1103 } |
1096 | 1104 |
1097 // emit body | 1105 // emit body |
1098 p->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); | 1106 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); |
1099 if(body) | 1107 if(body) |
1100 body->toIR(p); | 1108 body->toIR(p); |
1101 p->targetScopes.pop_back(); | 1109 p->func()->targetScopes.pop_back(); |
1102 | 1110 |
1103 if (!p->scopereturned()) | 1111 if (!p->scopereturned()) |
1104 llvm::BranchInst::Create(nextbb, p->scopebb()); | 1112 llvm::BranchInst::Create(nextbb, p->scopebb()); |
1105 | 1113 |
1106 // next | 1114 // next |
1189 v = p->ir->CreateSub(v, one); | 1197 v = p->ir->CreateSub(v, one); |
1190 DtoStore(v, keyval); | 1198 DtoStore(v, keyval); |
1191 } | 1199 } |
1192 | 1200 |
1193 // emit body | 1201 // emit body |
1194 p->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); | 1202 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,nextbb,endbb)); |
1195 if (body) | 1203 if (body) |
1196 body->toIR(p); | 1204 body->toIR(p); |
1197 p->targetScopes.pop_back(); | 1205 p->func()->targetScopes.pop_back(); |
1198 | 1206 |
1199 // jump to next iteration | 1207 // jump to next iteration |
1200 if (!p->scopereturned()) | 1208 if (!p->scopereturned()) |
1201 llvm::BranchInst::Create(nextbb, p->scopebb()); | 1209 llvm::BranchInst::Create(nextbb, p->scopebb()); |
1202 | 1210 |
1259 | 1267 |
1260 p->scope() = IRScope(labelBB,oldend); | 1268 p->scope() = IRScope(labelBB,oldend); |
1261 } | 1269 } |
1262 | 1270 |
1263 if (statement) { | 1271 if (statement) { |
1264 p->targetScopes.push_back(IRTargetScope(this,NULL,NULL,NULL)); | 1272 p->func()->targetScopes.push_back(IRTargetScope(this,NULL,NULL,NULL)); |
1265 statement->toIR(p); | 1273 statement->toIR(p); |
1266 p->targetScopes.pop_back(); | 1274 p->func()->targetScopes.pop_back(); |
1267 } | 1275 } |
1268 } | 1276 } |
1269 | 1277 |
1270 ////////////////////////////////////////////////////////////////////////////// | 1278 ////////////////////////////////////////////////////////////////////////////// |
1271 | 1279 |
1383 llsync = generate_unique_critical_section(); | 1391 llsync = generate_unique_critical_section(); |
1384 DtoEnterCritical(llsync); | 1392 DtoEnterCritical(llsync); |
1385 } | 1393 } |
1386 | 1394 |
1387 // emit body | 1395 // emit body |
1388 p->targetScopes.push_back(IRTargetScope(this,new EnclosingSynchro(this),NULL,NULL)); | 1396 p->func()->targetScopes.push_back(IRTargetScope(this,new EnclosingSynchro(this),NULL,NULL)); |
1389 body->toIR(p); | 1397 body->toIR(p); |
1390 p->targetScopes.pop_back(); | 1398 p->func()->targetScopes.pop_back(); |
1391 | 1399 |
1392 // exit lock | 1400 // exit lock |
1393 // no point in a unreachable unlock, terminating statements must insert this themselves. | 1401 // no point in a unreachable unlock, terminating statements must insert this themselves. |
1394 if (p->scopereturned()) | 1402 if (p->scopereturned()) |
1395 return; | 1403 return; |
1417 { | 1425 { |
1418 // load-store | 1426 // load-store |
1419 DtoMemoryBarrier(false, true, false, false); | 1427 DtoMemoryBarrier(false, true, false, false); |
1420 | 1428 |
1421 // do statement | 1429 // do statement |
1422 p->targetScopes.push_back(IRTargetScope(this,new EnclosingVolatile(this),NULL,NULL)); | 1430 p->func()->targetScopes.push_back(IRTargetScope(this,new EnclosingVolatile(this),NULL,NULL)); |
1423 statement->toIR(p); | 1431 statement->toIR(p); |
1424 p->targetScopes.pop_back(); | 1432 p->func()->targetScopes.pop_back(); |
1425 | 1433 |
1426 // no point in a unreachable barrier, terminating statements must insert this themselves. | 1434 // no point in a unreachable barrier, terminating statements must insert this themselves. |
1427 if (statement->blockExit() & BEfallthru) | 1435 if (statement->blockExit() & BEfallthru) |
1428 { | 1436 { |
1429 // store-load | 1437 // store-load |