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
use crate::collections::HashMap;
use crate::{DebugLabel, Hash, Item, Span};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct DebugInfo {
pub instructions: Vec<DebugInst>,
pub functions: HashMap<Hash, DebugSignature>,
pub functions_rev: HashMap<usize, Hash>,
}
impl DebugInfo {
pub fn instruction_at(&self, ip: usize) -> Option<&DebugInst> {
self.instructions.get(ip)
}
pub fn function_at(&self, ip: usize) -> Option<(Hash, &DebugSignature)> {
let hash = *self.functions_rev.get(&ip)?;
let signature = self.functions.get(&hash)?;
Some((hash, signature))
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DebugInst {
pub source_id: usize,
pub span: Span,
pub comment: Option<String>,
pub label: Option<DebugLabel>,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum DebugArgs {
EmptyArgs,
TupleArgs(usize),
Named(Vec<String>),
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DebugSignature {
pub path: Item,
pub args: DebugArgs,
}
impl DebugSignature {
pub fn new(path: Item, args: Vec<String>) -> Self {
Self {
path,
args: DebugArgs::Named(args),
}
}
}
impl fmt::Display for DebugSignature {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.args {
DebugArgs::EmptyArgs => {
write!(fmt, "{}", self.path)?;
}
DebugArgs::TupleArgs(args) if *args > 0 => {
write!(fmt, "{}(", self.path)?;
let mut it = 0..*args;
let last = it.next_back();
for arg in it {
write!(fmt, "{}, ", arg)?;
}
if let Some(arg) = last {
write!(fmt, "{}", arg)?;
}
write!(fmt, ")")?;
}
DebugArgs::Named(args) => {
write!(fmt, "{}(", self.path)?;
let mut it = args.iter();
let last = it.next_back();
for arg in it {
write!(fmt, "{}, ", arg)?;
}
if let Some(arg) = last {
write!(fmt, "{}", arg)?;
}
write!(fmt, ")")?;
}
_ => (),
}
Ok(())
}
}