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
use cairo_lang_casm::instructions::{
AssertEqInstruction, CallInstruction, Instruction, InstructionBody, JnzInstruction,
JumpInstruction,
};
use cairo_lang_casm::operand::{BinOpOperand, DerefOrImmediate, ResOperand};
use cairo_lang_sierra::program::StatementIdx;
type CodeOffset = usize;
#[derive(Debug, Eq, PartialEq)]
pub enum Relocation {
RelativeStatementId(StatementIdx),
EndOfProgram,
}
impl Relocation {
pub fn apply(
&self,
instruction_offset: CodeOffset,
statement_offsets: &[CodeOffset],
instruction: &mut Instruction,
) {
let target_pc = match self {
Relocation::RelativeStatementId(statement_idx) => statement_offsets[statement_idx.0],
Relocation::EndOfProgram => *statement_offsets.last().unwrap(),
};
match instruction {
Instruction {
body:
InstructionBody::Call(CallInstruction {
target: DerefOrImmediate::Immediate(value),
relative: true,
}),
inc_ap: false,
..
}
| Instruction {
body:
InstructionBody::Jnz(JnzInstruction {
jump_offset: DerefOrImmediate::Immediate(value),
condition: _,
}),
..
}
| Instruction {
body:
InstructionBody::Jump(JumpInstruction {
target: DerefOrImmediate::Immediate(value),
relative: true,
}),
inc_ap: false,
..
}
| Instruction {
body:
InstructionBody::AssertEq(AssertEqInstruction {
b:
ResOperand::BinOp(BinOpOperand {
b: DerefOrImmediate::Immediate(value),
..
}),
..
}),
..
} => {
value.value += target_pc as i128 - instruction_offset as i128;
}
_ => panic!("Bad relocation."),
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct RelocationEntry {
pub instruction_idx: CodeOffset,
pub relocation: Relocation,
}
pub fn relocate_instructions(
relocations: &[RelocationEntry],
statement_offsets: &[usize],
instructions: &mut [Instruction],
) {
let mut program_offset = 0;
let mut relocations_iter = relocations.iter();
let mut relocation_entry = relocations_iter.next();
for (instruction_idx, instruction) in instructions.iter_mut().enumerate() {
match relocation_entry {
Some(RelocationEntry { instruction_idx: relocation_idx, relocation })
if *relocation_idx == instruction_idx =>
{
relocation.apply(program_offset, statement_offsets, instruction);
relocation_entry = relocations_iter.next();
}
_ => (),
};
program_offset += instruction.body.op_size();
}
}