charm 0.0.1

ARM assembler & disassembler generated from the ARM exploration tools.
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Main instruction objects.

use super::config::*;
use super::consts::*;
use super::formatter::*;
use super::operand::*;
use crate::error::*;

/// Type that identifies the possible encoding of an instruction.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub enum Encoding {
    None,
    Alt1,
    Alt2,
    Alt3,
    Alt4,
    Alt5,
}

/// Type that represents an instruction.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub struct Instruction {
    ////TODO: change visibility
    pub code: Code,
    pub encoding: Encoding,
    pub mnemonic: Mnemonic,
    pub pc: u32,
    pub size: usize,
    pub operands: [Operand; MAX_OPERAND_COUNT],
}

impl Instruction {
    /// Instruction builder.
    pub fn builder(code: Code) -> InstructionBuilder {
        let mut instruction = Instruction::default();
        instruction.code = code;
        instruction.encoding = Encoding::None;
        instruction.mnemonic = code.mnemonic(&instruction);
        instruction.pc = 0;
        instruction.size = code.size(&instruction);
        InstructionBuilder { inner: instruction }
    }

    /// Creates an instruction with no operands.
    pub fn with_0(code: Code) -> Instruction {
        Instruction::builder(code).build()
    }

    /// Creates an instruction with one operand.
    pub fn with_1(code: Code, op0: impl Into<Operand>) -> Result<Instruction> {
        Ok(Instruction::builder(code).op0(op0)?.build())
    }

    /// Creates an instruction with two operands.
    pub fn with_2(
        code: Code,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder(code).op0(op0)?.op1(op1)?.build())
    }

    /// Creates an instruction with three operands.
    pub fn with_3(
        code: Code,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder(code)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .build())
    }

    /// Creates an instruction with four operands.
    pub fn with_4(
        code: Code,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
        op3: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder(code)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .op3(op3)?
            .build())
    }

    /// Creates an instruction with five operands.
    pub fn with_5(
        code: Code,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
        op3: impl Into<Operand>,
        op4: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder(code)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .op3(op3)?
            .op4(op4)?
            .build())
    }

    /// Creates an instruction with six operands.
    pub fn with_6(
        code: Code,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
        op3: impl Into<Operand>,
        op4: impl Into<Operand>,
        op5: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder(code)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .op3(op3)?
            .op4(op4)?
            .op5(op5)?
            .build())
    }

    /// Creates an instruction with seven operands.
    pub fn with_7(
        code: Code,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
        op3: impl Into<Operand>,
        op4: impl Into<Operand>,
        op5: impl Into<Operand>,
        op6: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder(code)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .op3(op3)?
            .op4(op4)?
            .op5(op5)?
            .op6(op6)?
            .build())
    }

    /// Creates an instruction with a given encoding and with no operands.
    pub fn with_encoding_0(code: Code, encoding: Encoding) -> Instruction {
        Instruction::builder_multi(code, encoding).build()
    }

    /// Creates an instruction with a given encoding and with no operands.
    pub fn with_encoding_1(
        code: Code,
        encoding: Encoding,
        op0: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder_multi(code, encoding).op0(op0)?.build())
    }

    /// Creates an instruction with a given encoding and with two operands.
    pub fn with_encoding_2(
        code: Code,
        encoding: Encoding,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder_multi(code, encoding)
            .op0(op0)?
            .op1(op1)?
            .build())
    }

    /// Creates an instruction with a given encoding and with three operands.
    pub fn with_encoding_3(
        code: Code,
        encoding: Encoding,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder_multi(code, encoding)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .build())
    }

    /// Creates an instruction with a given encoding and with four operands.
    pub fn with_encoding_4(
        code: Code,
        encoding: Encoding,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
        op3: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder_multi(code, encoding)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .op3(op3)?
            .build())
    }

    /// Creates an instruction with a given encoding and with five operands.
    pub fn with_encoding_5(
        code: Code,
        encoding: Encoding,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
        op3: impl Into<Operand>,
        op4: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder_multi(code, encoding)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .op3(op3)?
            .op4(op4)?
            .build())
    }

    /// Creates an instruction with a given encoding and with six operands.
    pub fn with_encoding_6(
        code: Code,
        encoding: Encoding,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
        op3: impl Into<Operand>,
        op4: impl Into<Operand>,
        op5: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder_multi(code, encoding)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .op3(op3)?
            .op4(op4)?
            .op5(op5)?
            .build())
    }

    /// Creates an instruction with a given encoding and with seven operands.
    pub fn with_encoding_7(
        code: Code,
        encoding: Encoding,
        op0: impl Into<Operand>,
        op1: impl Into<Operand>,
        op2: impl Into<Operand>,
        op3: impl Into<Operand>,
        op4: impl Into<Operand>,
        op5: impl Into<Operand>,
        op6: impl Into<Operand>,
    ) -> Result<Instruction> {
        Ok(Instruction::builder_multi(code, encoding)
            .op0(op0)?
            .op1(op1)?
            .op2(op2)?
            .op3(op3)?
            .op4(op4)?
            .op5(op5)?
            .op6(op6)?
            .build())
    }

    /// Builder for an instruction with multiple encodings.
    pub fn builder_multi(code: Code, encoding: Encoding) -> InstructionBuilder {
        let mut instruction = Instruction::default();
        instruction.code = code;
        instruction.encoding = encoding;
        instruction.mnemonic = code.mnemonic(&instruction);
        instruction.pc = 0;
        instruction.size = code.size(&instruction);
        InstructionBuilder { inner: instruction }
    }

    /// Returns the operand at `index`.
    pub fn get_op(&self, index: usize) -> &Operand {
        if index >= MAX_OPERAND_COUNT {
            todo!()
        }
        &self.operands[index]
    }

    /// Sets the operand at `index`.
    pub fn set_op(&mut self, index: usize, operand: impl Into<Operand>) -> Result<()> {
        if index >= MAX_OPERAND_COUNT {
            return Err(OperandError::OperandIndexOutOfRange(
                index,
                MAX_OPERAND_COUNT,
            ))?;
        }
        let operand = operand.into();
        self.code.check_op(index, &self, &operand)?;
        self.operands[index] = operand;
        Ok(())
    }

    /// Returns operand #0.
    pub fn op0(&self) -> &Operand {
        &self.operands[0]
    }

    /// Returns operand #1.
    pub fn op1(&self) -> &Operand {
        &self.operands[1]
    }

    /// Returns operand #2.
    pub fn op2(&self) -> &Operand {
        &self.operands[2]
    }

    /// Returns operand #3.
    pub fn op3(&self) -> &Operand {
        &self.operands[3]
    }

    /// Returns operand #4.
    pub fn op4(&self) -> &Operand {
        &self.operands[4]
    }

    /// Returns operand #5.
    pub fn op5(&self) -> &Operand {
        &self.operands[5]
    }

    /// Returns operand #6.
    pub fn op6(&self) -> &Operand {
        &self.operands[6]
    }

    /// Formats the instruction.
    pub fn format(
        &self,
        fmt: &mut impl Formatter,
        output: &mut impl FormatterOutput,
        config: &Config,
    ) -> Result<()> {
        self.code.format(self, fmt, output, config)
    }

    /// Returns the instruction condition type.
    pub fn condition(&self) -> ConditionalInstruction {
        self.code.condition(self)
    }
}

impl Default for Instruction {
    fn default() -> Self {
        Self {
            code: Code::Invalid,
            mnemonic: Mnemonic::Invalid,
            encoding: Encoding::None,
            operands: [Operand::default(); MAX_OPERAND_COUNT],
            pc: 0,
            size: 0,
        }
    }
}

/// Type that represents an instruction builder.
pub struct InstructionBuilder {
    inner: Instruction,
}

impl From<Instruction> for InstructionBuilder {
    fn from(value: Instruction) -> Self {
        Self { inner: value }
    }
}

impl InstructionBuilder {
    /// Sets the operand at `index`.
    pub fn operand(mut self, index: usize, operand: impl Into<Operand>) -> Result<Self> {
        if index >= MAX_OPERAND_COUNT {
            return Err(OperandError::OperandIndexOutOfRange(
                index,
                MAX_OPERAND_COUNT,
            ))?;
        }
        let operand = operand.into();
        self.inner.code.check_op(index, &self.inner, &operand)?;
        self.inner.operands[index] = operand;
        Ok(self)
    }

    /// Sets operand #0.
    pub fn op0(self, operand: impl Into<Operand>) -> Result<Self> {
        self.operand(0, operand)
    }

    /// Sets operand #1.
    pub fn op1(self, operand: impl Into<Operand>) -> Result<Self> {
        self.operand(1, operand)
    }

    /// Sets operand #2.
    pub fn op2(self, operand: impl Into<Operand>) -> Result<Self> {
        self.operand(2, operand)
    }

    /// Sets operand #3.
    pub fn op3(self, operand: impl Into<Operand>) -> Result<Self> {
        self.operand(3, operand)
    }

    /// Sets operand #4.
    pub fn op4(self, operand: impl Into<Operand>) -> Result<Self> {
        self.operand(4, operand)
    }

    /// Sets operand #5.
    pub fn op5(self, operand: impl Into<Operand>) -> Result<Self> {
        self.operand(5, operand)
    }

    /// Sets operand #6.
    pub fn op6(self, operand: impl Into<Operand>) -> Result<Self> {
        self.operand(6, operand)
    }

    /// Consumes the builder and creates the instruction.
    pub fn build(self) -> Instruction {
        self.inner
    }
}

/// Type that represents an element of an [`InstructionBlock`].
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum InstructionBlockElement {
    /// Contains an instruction.
    Instruction(Instruction),
    /// Contains a label.
    Label(u64),
}

impl From<Instruction> for InstructionBlockElement {
    fn from(value: Instruction) -> Self {
        InstructionBlockElement::Instruction(value)
    }
}

impl From<u64> for InstructionBlockElement {
    fn from(value: u64) -> Self {
        InstructionBlockElement::Label(value)
    }
}

/// Type that represents an instruction block, which is a list of consecutive instructions starting
/// from a given address.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Default)]
pub struct InstructionBlock {
    pub pc: u32,
    pub instructions: Vec<InstructionBlockElement>,
}

impl InstructionBlock {
    /// Creates a new instruction block starting at `pc`.
    pub fn new(pc: u32) -> Self {
        Self {
            pc,
            instructions: vec![],
        }
    }

    /// Creates an instruction block at address `pc` from a vector of instructions.
    pub fn with_instructions(pc: u32, instructions: Vec<InstructionBlockElement>) -> Self {
        Self { pc, instructions }
    }

    /// Adds an instruction to the end of the block.
    pub fn push_instruction(&mut self, instruction: Instruction) {
        self.instructions.push(instruction.into())
    }

    /// Adds a label to the end of the block.
    pub fn push_label(&mut self, label: u64) {
        self.instructions.push(label.into())
    }
}