-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpropagate_const.cpp
More file actions
110 lines (87 loc) · 1.87 KB
/
propagate_const.cpp
File metadata and controls
110 lines (87 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <unordered_set>
#include "common.h"
#include "Instruction.h"
#include "Machine.h"
#include "Expression.h"
#include "OpCode.h"
#include "dp_register.h"
/*
* const propagation.
*
*
* keep it simple for now -- stz only...
* stz %t0
* ...
* pei %t0 -> pea #0
* lda %t0 -> lda #0
* adc %t0 -> adc #0
* etc.
*
*/
// returns true if any changes made.
bool propagate_const(LineQueue &list) {
LineQueue optimized;
bool delta = false;
dp_register reg;
std::unordered_set<dp_register> known_zeroes;
for (auto iter = list.begin(); iter != list.end(); ++iter){
auto line = *iter;
OpCode opcode = line->opcode;
AddressMode mode = opcode.addressMode();
ExpressionPtr e = line->operands[0];
switch (opcode.mnemonic()) {
case STZ:
if (opcode.addressMode() == zp && e->is_temporary(reg)) {
known_zeroes.insert(reg);
}
break;
#if 0
case STA:
case STX:
case STY:
// rmw
case TSB:
case TRB:
case INC:
case DEC:
case ASL:
case LSR:
case ROR:
case ROL:
#endif
case PEI:
if (e->is_temporary(reg)) {
if (known_zeroes.find(reg) != known_zeroes.end()) {
line->opcode = OpCode(m65816, PEA, absolute);
line->operands[0] = Expression::Integer(0);
line->calc_registers();
delta = true;
}
}
break;
case LDA:
case LDX:
case LDY:
case ADC:
case SBC:
case EOR:
case AND:
case ORA:
if (opcode.addressMode() == zp && e->is_temporary(reg)) {
if (known_zeroes.find(reg) != known_zeroes.end()) {
line->opcode = OpCode(m65816, opcode.mnemonic(), immediate);
line->operands[0] = Expression::Integer(0);
line->calc_registers();
delta = true;
}
}
break;
default:
if (opcode.writes_zp() && opcode.addressMode() == zp && e->is_temporary(reg)) {
known_zeroes.erase(reg);
}
break;
}
}
return delta;
}