Skip to content

Commit 701d833

Browse files
feat: add initial version of new history events
Co-authored-by: SersemPeca <[email protected]>
1 parent aa37e95 commit 701d833

File tree

3 files changed

+138
-38
lines changed

3 files changed

+138
-38
lines changed

src/lib.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,64 @@ mod tests {
5656
}
5757
assert!(matches!(should_be_call, TraceLowLevelEvent::Call(CallRecord { .. })));
5858

59-
let int_value = ValueRecord::Int {
59+
let int_value_1 = ValueRecord::Int {
6060
i: 1,
6161
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
6262
};
6363
let int_value_2 = ValueRecord::Int {
6464
i: 2,
6565
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
6666
};
67-
tracer.register_variable_with_full_value("test_variable", int_value.clone());
67+
let int_value_3 = ValueRecord::Int {
68+
i: 3,
69+
type_id: tracer.ensure_type_id(TypeKind::Int, "Int"),
70+
};
71+
72+
tracer.register_variable_with_full_value("test_variable", int_value_1.clone());
6873

6974
let not_supported_value = ValueRecord::Error {
7075
msg: "not supported".to_string(),
7176
type_id: NONE_TYPE_ID,
7277
};
7378
tracer.register_variable_with_full_value("test_variable2", not_supported_value);
7479

75-
tracer.register_cell_value(ValueId(0), int_value);
80+
tracer.register_cell_value(Place(0), int_value_1.clone());
7681
let type_id = tracer.ensure_type_id(TypeKind::Seq, "Vector<Int>");
7782
tracer.register_compound_value(
78-
ValueId(1),
83+
Place(1),
7984
ValueRecord::Sequence {
80-
elements: vec![ValueRecord::Cell { value_id: ValueId(0) }], // #0
85+
elements: vec![ValueRecord::Cell { place: Place(0) }], // #0
8186
type_id,
8287
},
8388
);
84-
tracer.register_variable("test_variable3", ValueId(1));
85-
tracer.assign_cell(ValueId(1), int_value_2.clone());
86-
tracer.register_cell_value(ValueId(2), int_value_2.clone());
87-
tracer.assign_compound_item(ValueId(0), 0, ValueId(2));
89+
tracer.register_variable("test_variable3", Place(1));
90+
tracer.assign_cell(Place(1), int_value_2.clone());
91+
tracer.register_cell_value(Place(2), int_value_2.clone());
92+
tracer.assign_compound_item(Place(0), 0, Place(2));
8893

8994
tracer.register_return(NONE_VALUE);
9095
tracer.drop_variable("test_variable3");
9196

97+
// example of the history events
98+
tracer.bind_variable("variable1", Place(1));
99+
tracer.bind_variable("variable2", Place(2));
100+
tracer.bind_variable("variable3", Place(3));
101+
102+
tracer.register_variable_with_full_value("variable1", int_value_1.clone());
103+
tracer.register_variable_with_full_value("variable2", int_value_2.clone());
104+
tracer.register_variable_with_full_value("variable3", int_value_3.clone());
105+
106+
// tracer.assign_simple("variable1", "variable2", PassBy::Value);
107+
// tracer.assign_compound("variable1", &["variable2", "variable3"], PassBy::Value);
108+
109+
// more future-proof hopefully, if we add other kinds of RValue
110+
let rvalue_1 = tracer.simple_rvalue("variable2");
111+
tracer.assign("variable1", rvalue_1, PassBy::Value);
112+
let rvalue_2 = tracer.compound_rvalue(&["variable2".to_string(), "variable3".to_string()]);
113+
tracer.assign("variable1", rvalue_2, PassBy::Value);
114+
115+
tracer.drop_variables(&["variable1".to_string(), "variable2".to_string(), "variable3".to_string()]);
116+
92117
assert_eq!(tracer.events.len(), 34);
93118
// visible with
94119
// cargo tets -- --nocapture

src/tracer.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use std::fs;
55
use std::path::{Path, PathBuf};
66

77
use crate::types::{
8-
AssignCellRecord, AssignCompoundItemRecord, CallRecord, CellValueRecord, CompoundValueRecord, EventLogKind, FullValueRecord, FunctionId,
9-
FunctionRecord, Line, PathId, RecordEvent, ReturnRecord, StepRecord, TraceLowLevelEvent, TraceMetadata, TypeId, TypeKind, TypeRecord,
10-
TypeSpecificInfo, ValueId, ValueRecord, VariableCellRecord, VariableId,
8+
AssignCellRecord, AssignCompoundItemRecord, AssignmentRecord, CallRecord, CellValueRecord, CompoundValueRecord, EventLogKind, FullValueRecord,
9+
FunctionId, FunctionRecord, Line, PassBy, PathId, Place, RecordEvent, ReturnRecord, StepRecord, TraceLowLevelEvent, TraceMetadata, TypeId,
10+
TypeKind, TypeRecord, TypeSpecificInfo, ValueRecord, VariableCellRecord, VariableId,
1111
};
12+
use crate::RValue;
1213

1314
pub struct Tracer {
1415
// trace metadata:
@@ -119,7 +120,7 @@ impl Tracer {
119120

120121
pub fn register_step(&mut self, path: &Path, line: Line) {
121122
let path_id = self.ensure_path_id(path);
122-
self.events.push(TraceLowLevelEvent::Step(StepRecord { path_id, line}));
123+
self.events.push(TraceLowLevelEvent::Step(StepRecord { path_id, line }));
123124
}
124125

125126
pub fn register_call(&mut self, function_id: FunctionId, args: Vec<FullValueRecord>) {
@@ -182,37 +183,62 @@ impl Tracer {
182183
self.events.push(TraceLowLevelEvent::Value(FullValueRecord { variable_id, value }));
183184
}
184185

185-
pub fn register_compound_value(&mut self, value_id: ValueId, value: ValueRecord) {
186-
self.events
187-
.push(TraceLowLevelEvent::CompoundValue(CompoundValueRecord { value_id, value }));
186+
pub fn register_compound_value(&mut self, place: Place, value: ValueRecord) {
187+
self.events.push(TraceLowLevelEvent::CompoundValue(CompoundValueRecord { place, value }));
188188
}
189189

190-
pub fn register_cell_value(&mut self, value_id: ValueId, value: ValueRecord) {
191-
self.events.push(TraceLowLevelEvent::CellValue(CellValueRecord { value_id, value }));
190+
pub fn register_cell_value(&mut self, place: Place, value: ValueRecord) {
191+
self.events.push(TraceLowLevelEvent::CellValue(CellValueRecord { place, value }));
192192
}
193193

194-
pub fn assign_compound_item(&mut self, value_id: ValueId, index: usize, item_value_id: ValueId) {
194+
pub fn assign_compound_item(&mut self, place: Place, index: usize, item_place: Place) {
195195
self.events.push(TraceLowLevelEvent::AssignCompoundItem(AssignCompoundItemRecord {
196-
value_id,
196+
place,
197197
index,
198-
item_value_id,
198+
item_place,
199199
}));
200200
}
201-
pub fn assign_cell(&mut self, value_id: ValueId, new_value: ValueRecord) {
202-
self.events.push(TraceLowLevelEvent::AssignCell(AssignCellRecord { value_id, new_value }));
201+
pub fn assign_cell(&mut self, place: Place, new_value: ValueRecord) {
202+
self.events.push(TraceLowLevelEvent::AssignCell(AssignCellRecord { place, new_value }));
203203
}
204204

205-
pub fn register_variable(&mut self, variable_name: &str, value_id: ValueId) {
205+
pub fn register_variable(&mut self, variable_name: &str, place: Place) {
206206
let variable_id = self.ensure_variable_id(variable_name);
207207
self.events
208-
.push(TraceLowLevelEvent::VariableCell(VariableCellRecord { variable_id, value_id }));
208+
.push(TraceLowLevelEvent::VariableCell(VariableCellRecord { variable_id, place }));
209209
}
210210

211211
pub fn drop_variable(&mut self, variable_name: &str) {
212212
let variable_id = self.ensure_variable_id(variable_name);
213213
self.events.push(TraceLowLevelEvent::DropVariable(variable_id));
214214
}
215215

216+
// history event helpers
217+
pub fn assign(&mut self, variable_name: &str, rvalue: RValue, pass_by: PassBy) {
218+
let variable_id = self.ensure_variable_id(variable_name);
219+
self.events.push(TraceLowLevelEvent::Assignment(AssignmentRecord {
220+
to: variable_id,
221+
from: rvalue,
222+
pass_by,
223+
}));
224+
}
225+
226+
pub fn bind_variable(&mut self, variable_name: &str, place: Place) {
227+
unimplemented!()
228+
}
229+
230+
pub fn drop_variables(&mut self, variable_names: &[String]) {
231+
unimplemented!()
232+
}
233+
234+
pub fn simple_rvalue(&mut self, variable_name: &str) -> RValue {
235+
todo!()
236+
}
237+
238+
pub fn compound_rvalue(&mut self, variable_dependencies: &[String]) -> RValue {
239+
todo!()
240+
}
241+
216242
pub fn drop_last_step(&mut self) {
217243
self.events.push(TraceLowLevelEvent::DropLastStep);
218244
}

src/types.rs

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ pub enum TraceLowLevelEvent {
2626
Event(RecordEvent),
2727
Asm(Vec<String>),
2828

29+
// events useful for history
30+
BindVariable(BindVariableRecord), // bind a variable to a certain place
31+
Assignment(AssignmentRecord), // assigning or passing by params
32+
DropVariables(Vec<VariableId>), // dropping variables e.g. in the end of scope/heap lifetime
33+
2934
// experimental modification value tracking events
35+
// probably will be reworked or replaced by the newer
36+
// history events with some additions
37+
// for now here for backward compatibility/experiments
3038
CompoundValue(CompoundValueRecord),
3139
CellValue(CellValueRecord),
3240
AssignCompoundItem(AssignCompoundItemRecord),
@@ -39,42 +47,83 @@ pub enum TraceLowLevelEvent {
3947
DropLastStep,
4048
}
4149

50+
#[derive(Debug, Clone, Serialize, Deserialize)]
51+
pub struct BindVariableRecord {
52+
pub variable_id: VariableId,
53+
pub place: Place,
54+
}
55+
56+
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
57+
pub enum PassBy {
58+
#[default]
59+
Value,
60+
Reference,
61+
// TODO: languages with more special ways of passing
62+
}
63+
64+
// used for all kinds of by value/by ref assignment/passing
65+
// * assignments
66+
// * arg(parameter) passing
67+
#[derive(Debug, Clone, Serialize, Deserialize)]
68+
pub struct AssignmentRecord {
69+
pub to: VariableId,
70+
pub pass_by: PassBy,
71+
pub from: RValue,
72+
}
73+
74+
#[derive(Debug, Clone, Serialize, Deserialize)]
75+
#[serde(tag = "kind")]
76+
pub enum RValue {
77+
Simple(VariableId),
78+
// eventually in future:
79+
// discuss more: Const(String, ValueRecord),
80+
Compound(Vec<RValue>),
81+
}
82+
4283
#[derive(Debug, Clone, Serialize, Deserialize)]
4384
pub struct CompoundValueRecord {
44-
pub value_id: ValueId,
85+
pub place: Place,
4586
pub value: ValueRecord,
4687
}
4788

4889
#[derive(Debug, Clone, Serialize, Deserialize)]
4990
pub struct CellValueRecord {
50-
pub value_id: ValueId,
91+
pub place: Place,
5192
pub value: ValueRecord,
5293
}
5394

5495
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
5596
pub struct AssignCompoundItemRecord {
56-
pub value_id: ValueId,
97+
pub place: Place,
5798
pub index: usize,
58-
pub item_value_id: ValueId,
99+
pub item_place: Place,
59100
}
60101

61102
#[derive(Debug, Clone, Serialize, Deserialize)]
62103
pub struct AssignCellRecord {
63-
pub value_id: ValueId,
104+
pub place: Place,
64105
pub new_value: ValueRecord,
65106
}
66107

67108
#[derive(Debug, Clone, Serialize, Deserialize)]
68109
pub struct VariableCellRecord {
69110
pub variable_id: VariableId,
70-
pub value_id: ValueId,
71-
}
72-
73-
// for now can be both just an index and
74-
// a 64-bit pointer; think if we need
75-
// something more general?
111+
pub place: Place,
112+
}
113+
114+
// opaque(?) id:
115+
// can be anything, depending on the lang
116+
// and its implementation
117+
// usually we expects it's
118+
// * some kind of pointer/address
119+
// * some kind of internal index(interpreter or stack)
120+
// * some other kind of id which somehow
121+
// uniquely represents the "place" of this variable
122+
// it's useful to let us track things on the more direct value
123+
// level/things like aliasing/mutable variables in different frames
124+
// history of mutations to a value etc
76125
#[derive(Hash, Debug, Default, Copy, Clone, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
77-
pub struct ValueId(pub usize);
126+
pub struct Place(pub i64);
78127

79128
#[derive(Debug, Clone, Serialize, Deserialize)]
80129
pub struct FullValueRecord {
@@ -306,7 +355,7 @@ pub enum ValueRecord {
306355
type_id: TypeId,
307356
},
308357
Cell {
309-
value_id: ValueId,
358+
place: Place,
310359
},
311360
}
312361

0 commit comments

Comments
 (0)