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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use arch::pic17::PartialInstructionContext;
use arch::pic17::SFRS;
use yaxpeax_pic17::{Instruction, Opcode, Operand};

pub fn updates_of<T>(instr: &Instruction, ctx: &T) -> Vec<Update> where T: PartialInstructionContext {
    fn register_updates<T>(op: Operand, ctx: &T) -> Vec<Update> where T: PartialInstructionContext {
        use arch::pic17::cpu::try_debank;
        match op {
            Operand::W => { vec![Update::W] },
            Operand::File(addr) => {
                let initial_addr = try_debank(addr, Some(ctx));
                let inner_addr = initial_addr.and_then(|debanked| match debanked {
                    SFRS::INDF0 => { ctx.memory(SFRS::FSR0).and_then(|addr| { try_debank(addr, Some(ctx)) }) },
                    SFRS::INDF1 => { ctx.memory(SFRS::FSR1).and_then(|addr| { try_debank(addr, Some(ctx)) }) },
                    x @ _ => { Some(x) }
                });
                match (initial_addr, inner_addr) {
                    (None, _) |
                    (_, None) => { vec![Update::Unknown] },
                    (_, Some(SFRS::INDF0)) |
                    (_, Some(SFRS::INDF1)) => { vec![] },
                    (Some(SFRS::INDF0), Some(x)) |
                    (Some(SFRS::INDF1), Some(x)) => { vec![Update::Memory(x)] },
                    (Some(x), Some(_)) => { vec![Update::Memory(x)] }
                }
            },
            Operand::Nothing => { vec![] },
            _ => { unreachable!() }
        }
    }
    match instr.opcode {
        Opcode::GOTO |
        Opcode::CALL |
        Opcode::SLEEP |
        Opcode::CLRWDT | // TODO: Verify.
        Opcode::NOP => { vec![] }
        Opcode::MOVLW |
        Opcode::RETLW => {
            vec![Update::W]
        },
        Opcode::MOVLB => { vec![Update::BSR_SFR] },
        Opcode::MOVLR => { vec![Update::BSR_GPR] },
        Opcode::RETURN |
        Opcode::RETFIE => { vec![] } // TODO: stack??
        Opcode::RRCF |
        Opcode::RLCF |
        Opcode::SUBWFB |
        Opcode::ADDWFC => {
            let mut updates = register_updates(instr.operands[1], ctx);
            updates.push(Update::Carry);
            updates
        }
        Opcode::LCALL => {
            vec![]
        }
        Opcode::MOVWF => {
            register_updates(instr.operands[0], ctx)
        }
        Opcode::MOVFP |
        Opcode::MOVPF => {
            // TODO: handle cases where, say, source is INDF_ with inc/dec
            register_updates(instr.operands[1], ctx)
        }
        Opcode::ADDLW |
        Opcode::SUBLW |
        Opcode::IORLW |
        Opcode::XORLW |
        Opcode::ANDLW => {
            vec![Update::W, Update::Carry]
        }
        Opcode::MULLW => {
            vec![Update::W, Update::Memory(SFRS::PRODL), Update::Memory(SFRS::PRODH)]
        }
        Opcode::TSTFSZ |
        Opcode::BTFSS |
        Opcode::BTFSC => {
            // TODO: handle cases where source is INDF_ with inc/dec
            vec![]
        }

        Opcode::BTG |
        Opcode::BSF |
        Opcode::BCF => {
            register_updates(instr.operands[0], ctx)
        }

        Opcode::CLRF => {
            let mut updates = register_updates(instr.operands[0], ctx);
            match instr.operands[1] {
                Operand::W => {
                    updates.push(Update::W);
                }
                _ => { }
            };
            updates
        }
        Opcode::TLRDL |
        Opcode::TLRDH |
        Opcode::TABLRDL |
        Opcode::TABLRDH => {
            register_updates(instr.operands[0], ctx)
        }
        Opcode::TABLRDLI |
        Opcode::TABLRDHI => {
            let mut dest = register_updates(instr.operands[0], ctx);
            dest.push(Update::Memory(SFRS::TBLPTRL));
            dest.push(Update::Memory(SFRS::TBLPTRH));
            dest
        }
        Opcode::TLWTL |
        Opcode::TLWTH |
        Opcode::TABLWTL |
        Opcode::TABLWTH => {
            vec![]
        },
        Opcode::TABLWTLI |
        Opcode::TABLWTHI => {
            vec![Update::Memory(SFRS::TBLPTRL), Update::Memory(SFRS::TBLPTRH)]
        }

        Opcode::INFSNZ |
        Opcode::DCFSNZ |
        Opcode::DECFSZ |
        Opcode::INCFSZ |
        Opcode::DECF |
        Opcode::INCF |
        Opcode::SUBWF |
        Opcode::IORWF |
        Opcode::ANDWF |
        Opcode::XORWF |
        Opcode::ADDWF => {
            // TODO: C, Z,...
            register_updates(instr.operands[1], ctx)
        }
        Opcode::MULWF => {
            vec![Update::Memory(SFRS::PRODL), Update::Memory(SFRS::PRODH)]
        }

        Opcode::NEGW |
        Opcode::CPFSLT |
        Opcode::CPFSEQ |
        Opcode::CPFSGT |
        Opcode::COMF |
        Opcode::SWAPF |
        Opcode::RRNCF |
        Opcode::RLNCF |
        Opcode::SETF |

        Opcode::DAW | // TODO: yeah, phoning this one in.
        Opcode::Invalid(_, _) => {
            // TODO: more precise definitions here..
            vec![Update::Unknown]
        }
    }
}

// TODO: Failing a symbolic implementation of CPU,
// the next best thing is being able to identify all src/dep of instructions...
// TODO: does this add gross indirection to accesses of ctx.<member>()?
//          f.ex bsr_sfr() should be inline-able...
pub fn dependencies_of<T: PartialInstructionContext>(instr: &Instruction, ctx: &T) -> Vec<Dependence> {
    fn register_write_deps<T: PartialInstructionContext>(addr: u8, ctx: &T) -> Vec<Dependence> {
        if addr < 0x10 {
            if addr == 0x00 {
                vec![Dependence::Memory(SFRS::FSR0), Dependence::BSR_GPR]
            } else if addr == 0x08 {
                vec![Dependence::Memory(SFRS::FSR1), Dependence::BSR_GPR]
            } else {
                vec![]
            }
        } else if addr < 0x20 {
            ctx.bsr_sfr().map(|_| {
                vec![]
            }).unwrap_or_else(|| {
                vec![Dependence::BSR_SFR, Dependence::Unknown]
            })
        } else {
            ctx.bsr_gpr().map(|_| {
                vec![]
            }).unwrap_or_else(|| {
                vec![Dependence::BSR_GPR, Dependence::Unknown]
            })
        }
    }
    fn register_deps(addr: u8, ctx: &dyn PartialInstructionContext) -> Vec<Dependence> {
        if addr < 0x10 {
            if addr == 0x00 {
                vec![Dependence::Memory(SFRS::FSR0), Dependence::BSR_GPR]
            } else if addr == 0x08 {
                vec![Dependence::Memory(SFRS::FSR1), Dependence::BSR_GPR]
            } else {
                vec![Dependence::Memory(addr as u16)]
            }
        } else if addr < 0x20 {
            ctx.bsr_sfr().map(|bsr| {
                let full_addr = addr as u16 | ((bsr as u16) << 8);
                vec![Dependence::Memory(full_addr)]
            }).unwrap_or_else(|| {
                vec![Dependence::BSR_SFR, Dependence::Unknown]
            })
        } else {
            ctx.bsr_gpr().map(|bsr| {
                let full_addr = addr as u16 | ((bsr as u16) << 8);
                vec![Dependence::Memory(full_addr)]
            }).unwrap_or_else(|| {
                vec![Dependence::BSR_GPR, Dependence::Unknown]
            })
        }
    }
    match instr.opcode {
        Opcode::GOTO |
        Opcode::CALL |
        Opcode::MOVLB |
        Opcode::MOVLR |
        Opcode::MOVLW |
        Opcode::SLEEP |
        Opcode::CLRWDT | // TODO: Verify.
        Opcode::NOP => { vec![] }
        Opcode::RETLW |
        Opcode::RETURN |
        Opcode::RETFIE => { vec![Dependence::Stack(0)] }
        Opcode::SUBWFB |
        Opcode::ADDWFC => {
            let mut deps = register_deps(instr.operands[0].file_value(), ctx);
            deps.push(Dependence::W);
            deps.push(Dependence::Carry);
            deps
        }
        Opcode::RRCF |
        Opcode::RLCF => {
            let mut deps = register_deps(instr.operands[0].file_value(), ctx);
            deps.push(Dependence::Carry);
            deps
        }
        Opcode::LCALL => {
            vec![Dependence::Memory(SFRS::PCLATH)]
        }
        Opcode::MOVWF => {
            let mut deps = register_write_deps(instr.operands[0].file_value(), ctx);
            deps.push(Dependence::W);
            deps
        }
        Opcode::NEGW |
        Opcode::CPFSLT |
        Opcode::CPFSEQ |
        Opcode::CPFSGT |
        Opcode::SUBWF |
        Opcode::IORWF |
        Opcode::ANDWF |
        Opcode::XORWF |
        Opcode::ADDWF |
        Opcode::MULWF => {
            let mut deps = register_deps(instr.operands[0].file_value(), ctx);
            deps.push(Dependence::W);
            deps
        }
        Opcode::DECF |
        Opcode::COMF |
        Opcode::INCF |
        Opcode::DECFSZ |
        Opcode::SWAPF |
        Opcode::INCFSZ |
        Opcode::RRNCF |
        Opcode::RLNCF |
        Opcode::INFSNZ |
        Opcode::DCFSNZ |
        Opcode::CLRF |
        Opcode::SETF => {
            register_deps(instr.operands[0].file_value(), ctx)
        }

        Opcode::BTG |
        Opcode::TSTFSZ |
        Opcode::BSF |
        Opcode::BCF |
        Opcode::BTFSS |
        Opcode::BTFSC => {
            register_deps(instr.operands[0].file_value(), ctx)
        }

        Opcode::MOVFP |
        Opcode::MOVPF => {
            let mut op0_deps = register_deps(instr.operands[0].file_value(), ctx);
            let mut op1_deps = register_write_deps(instr.operands[1].file_value(), ctx);
            op0_deps.append(&mut op1_deps);
            op0_deps
        }

        Opcode::ADDLW |
        Opcode::SUBLW |
        Opcode::IORLW |
        Opcode::XORLW |
        Opcode::ANDLW |
        Opcode::MULLW => {
            vec![Dependence::W]
        }

        Opcode::DAW | // TODO: yeah, phoning this one in.
        Opcode::Invalid(_, _) |
        Opcode::TLRDL |
        Opcode::TLRDH |
        Opcode::TLWTL |
        Opcode::TLWTH |
        Opcode::TABLRDL |
        Opcode::TABLRDLI |
        Opcode::TABLRDH |
        Opcode::TABLRDHI |
        Opcode::TABLWTL |
        Opcode::TABLWTLI |
        Opcode::TABLWTH |
        Opcode::TABLWTHI => {
            // TODO: more precise definitions here..
            vec![Dependence::Unknown]
        }
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, PartialEq, Clone, Copy, Eq, Hash, Serialize, Deserialize)]
pub enum Dependence {
    W, // aka the WREG SFR, knowing this should imply knowing that memory and vice versa
    Memory(u16), // just as the CPU, linear memory. may be SFR or data.
    Program(u16), // some word of program memory (tblrd)
    Stack(u8), // some value off the stack (return). index is relative to TOS.
    Carry, // depends on the carry bit. we might know this while not knowing ALUSTA.
    BSR_SFR, // the half of BSR used for bank selection of SFRs.
    BSR_GPR, // the half of BSR used for bank selection of general memory.
    Unknown // depends on something, we don't know what.
}

#[allow(non_camel_case_types)]
#[derive(Debug)]
pub enum Update {
    W,
    Memory(u16),
    Program(u16),
    Carry,
    BSR_SFR,
    BSR_GPR,
    Unknown
}