543
554
if(!debug['R'] || debug['P'])
557
if(debug['R'] && debug['v']) {
558
print("after pass 7 (peep)\n");
559
for(r=firstr; r; r=r->link)
560
print("%04d %P\n", r->pc, r->prog);
1286
1312
return bitno(b) + D_AX;
1315
/* what instruction does a JMP to p eventually land on? */
1317
chasejmp(Reg *r, int *jmploop)
1323
if(r->prog->as != AJMP || r->prog->to.type != D_BRANCH)
1333
/* mark all code reachable from firstp as alive */
1340
for(r=firstr; r; r=r->link) {
1345
if(p->as != ACALL && p->to.type == D_BRANCH)
1347
if(p->as == AJMP || p->as == ARET || p->as == AUNDEF)
1353
* the code generator depends on being able to write out JMP
1354
* instructions that it can jump to now but fill in later.
1355
* the linker will resolve them nicely, but they make the code
1356
* longer and more difficult to follow during debugging.
1366
if(debug['R'] && debug['v'])
1367
print("\nfixjmp\n");
1369
// pass 1: resolve jump to AJMP, mark all code as dead.
1371
for(r=firstr; r; r=r->link) {
1373
if(debug['R'] && debug['v'])
1374
print("%04d %P\n", r->pc, p);
1375
if(p->as != ACALL && p->to.type == D_BRANCH && r->s2 && r->s2->prog->as == AJMP) {
1376
r->s2 = chasejmp(r->s2, &jmploop);
1377
p->to.offset = r->s2->pc;
1378
if(debug['R'] && debug['v'])
1383
if(debug['R'] && debug['v'])
1386
// pass 2: mark all reachable code alive
1389
// pass 3: delete dead code (mostly JMPs).
1390
for(r=firstr; r; r=r->link) {
1393
if(p->link == P && p->as == ARET && r->p1 && r->p1->prog->as != ARET) {
1394
// This is the final ARET, and the code so far doesn't have one.
1397
if(debug['R'] && debug['v'])
1398
print("del %04d %P\n", r->pc, p);
1404
// pass 4: elide JMP to next instruction.
1405
// only safe if there are no jumps to JMPs anymore.
1407
for(r=firstr; r; r=r->link) {
1409
if(p->as == AJMP && p->to.type == D_BRANCH && r->s2 == r->link) {
1410
if(debug['R'] && debug['v'])
1411
print("del %04d %P\n", r->pc, p);
1417
// fix back pointers.
1418
for(r=firstr; r; r=r->link) {
1422
for(r=firstr; r; r=r->link) {
1424
r->p2link = r->s2->p2;
1429
if(debug['R'] && debug['v']) {
1431
for(r=firstr; r; r=r->link)
1432
print("%04d %P\n", r->pc, r->prog);