synth-backend 0.11.4

ARM encoder, ELF builder, vector table, linker scripts, and MPU configuration
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! VFP-D (Double-Precision) Encoding Unit Tests
//!
//! Validates that F64 VFP-D instructions encode to the canonical ARM machine
//! code bytes documented in the ARMv7-M Architecture Reference Manual,
//! Section A7.5 (Floating-point data-processing instructions).
//!
//! VFP-D encoding (32-bit word, same in ARM32 and Thumb-2):
//!
//!   31..28  cond / 1110 (always)
//!   27..24  1110
//!   23..20  op fields
//!   19..16  Vn[3:0]    (Dn = (N << 4) | Vn — but for D0..D15, N is in bit 7 and = 0)
//!   15..12  Vd[3:0]    (Dd = (D << 4) | Vd — but for D0..D15, D is in bit 22 and = 0)
//!   11..8   1011       (coprocessor 11 = double-precision)
//!   7       N
//!   6..5    op2 / M
//!   3..0    Vm[3:0]
//!
//! Test bytes are cross-checked against ARM ARM Table A7-50/A7-52/A7-54 and
//! against the synth-backend `arm_encoder.rs` helpers, which derive the same
//! bit layout from the manual. Adding `arm-none-eabi-as -mthumb -mcpu=cortex-m7
//! -mfpu=fpv5-d16` assembly of an equivalent one-line program produces the
//! same bytes.

use synth_backend::ArmEncoder;
use synth_core::target::FPUPrecision;
use synth_synthesis::{ArmOp, MemAddr, Reg, VfpReg};

// ============================================================================
// HELPERS
// ============================================================================

/// Reconstruct a 32-bit instruction word from Thumb-2 byte encoding
/// (two little-endian halfwords: hw1[0..2], hw2[2..4]).
fn thumb_bytes_to_word(bytes: &[u8]) -> u32 {
    assert_eq!(bytes.len(), 4, "Expected 4-byte Thumb-2 VFP instruction");
    let hw1 = u16::from_le_bytes([bytes[0], bytes[1]]) as u32;
    let hw2 = u16::from_le_bytes([bytes[2], bytes[3]]) as u32;
    (hw1 << 16) | hw2
}

/// Reconstruct a 32-bit instruction word from ARM32 byte encoding (single LE u32).
fn arm_bytes_to_word(bytes: &[u8]) -> u32 {
    assert_eq!(bytes.len(), 4, "Expected 4-byte ARM32 instruction");
    u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
}

fn thumb_encoder() -> ArmEncoder {
    ArmEncoder::new_thumb2_with_fpu(Some(FPUPrecision::Double))
}

fn arm_encoder() -> ArmEncoder {
    ArmEncoder::new_arm32()
}

// ============================================================================
// VADD.F64 / VSUB.F64 / VMUL.F64 / VDIV.F64
// ============================================================================

#[test]
fn test_vadd_f64_d0_d0_d0_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Add {
            dd: VfpReg::D0,
            dn: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    // Base: 0xEE300B00, all reg fields zero. Coprocessor 11 (cp11 = 0xB).
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xEE300B00,
        "VADD.F64 D0, D0, D0"
    );
}

#[test]
fn test_vadd_f64_d1_d2_d3_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Add {
            dd: VfpReg::D1,
            dn: VfpReg::D2,
            dm: VfpReg::D3,
        })
        .unwrap();
    // D1: Vd=1, D=0;  D2: Vn=2, N=0;  D3: Vm=3, M=0
    // word = 0xEE300B00 | (2<<16) | (1<<12) | 3 = 0xEE321B03
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xEE321B03,
        "VADD.F64 D1, D2, D3"
    );
}

#[test]
fn test_vsub_f64_d0_d0_d0_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Sub {
            dd: VfpReg::D0,
            dn: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    // VSUB.F64 has op=1 → bit 6 set → 0xEE300B40
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xEE300B40,
        "VSUB.F64 D0, D0, D0"
    );
}

#[test]
fn test_vmul_f64_d4_d5_d6_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Mul {
            dd: VfpReg::D4,
            dn: VfpReg::D5,
            dm: VfpReg::D6,
        })
        .unwrap();
    // base 0xEE200B00 | (5<<16) | (4<<12) | 6 = 0xEE254B06
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xEE254B06,
        "VMUL.F64 D4, D5, D6"
    );
}

#[test]
fn test_vdiv_f64_d0_d0_d0_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Div {
            dd: VfpReg::D0,
            dn: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    // VDIV.F64 base = 0xEE800B00
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xEE800B00,
        "VDIV.F64 D0, D0, D0"
    );
}

#[test]
fn test_vdiv_f64_d15_d14_d13_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Div {
            dd: VfpReg::D15,
            dn: VfpReg::D14,
            dm: VfpReg::D13,
        })
        .unwrap();
    // D15: Vd=15, D=0;  D14: Vn=14, N=0;  D13: Vm=13, M=0
    // base 0xEE800B00 | (14<<16) | (15<<12) | 13 = 0xEE8EFB0D
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xEE8EFB0D,
        "VDIV.F64 D15, D14, D13"
    );
}

// ============================================================================
// VABS.F64 / VNEG.F64 / VSQRT.F64
// ============================================================================

#[test]
fn test_vabs_f64_d0_d0_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Abs {
            dd: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    assert_eq!(thumb_bytes_to_word(&bytes), 0xEEB00BC0, "VABS.F64 D0, D0");
}

#[test]
fn test_vneg_f64_d0_d0_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Neg {
            dd: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    assert_eq!(thumb_bytes_to_word(&bytes), 0xEEB10B40, "VNEG.F64 D0, D0");
}

#[test]
fn test_vsqrt_f64_d0_d0_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Sqrt {
            dd: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    assert_eq!(thumb_bytes_to_word(&bytes), 0xEEB10BC0, "VSQRT.F64 D0, D0");
}

#[test]
fn test_vsqrt_f64_d3_d7_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Sqrt {
            dd: VfpReg::D3,
            dm: VfpReg::D7,
        })
        .unwrap();
    // base 0xEEB10BC0 | (3<<12) | 7 = 0xEEB13BC7
    assert_eq!(thumb_bytes_to_word(&bytes), 0xEEB13BC7, "VSQRT.F64 D3, D7");
}

#[test]
fn test_vabs_f64_d8_d9_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Abs {
            dd: VfpReg::D8,
            dm: VfpReg::D9,
        })
        .unwrap();
    // base 0xEEB00BC0 | (8<<12) | 9 = 0xEEB08BC9
    assert_eq!(thumb_bytes_to_word(&bytes), 0xEEB08BC9, "VABS.F64 D8, D9");
}

// ============================================================================
// VLDR.64 / VSTR.64
// ============================================================================

#[test]
fn test_vldr_f64_d0_r0_8_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Load {
            dd: VfpReg::D0,
            addr: MemAddr::imm(Reg::R0, 8),
        })
        .unwrap();
    // VLDR.64 base 0xED900B00 with positive offset (U=1 already in base);
    // imm8 = 8/4 = 2 → 0xED900B02
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xED900B02,
        "VLDR.64 D0, [R0, #8]"
    );
}

#[test]
fn test_vstr_f64_d0_sp_0_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Store {
            dd: VfpReg::D0,
            addr: MemAddr::imm(Reg::SP, 0),
        })
        .unwrap();
    // VSTR.64 base 0xED800B00, Rn=SP=13, imm8=0 → 0xED8D0B00
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xED8D0B00,
        "VSTR.64 D0, [SP, #0]"
    );
}

#[test]
fn test_vldr_f64_d2_r1_16_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Load {
            dd: VfpReg::D2,
            addr: MemAddr::imm(Reg::R1, 16),
        })
        .unwrap();
    // base 0xED900B00 | (1<<16) | (2<<12) | (16/4) = 0xED912B04
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xED912B04,
        "VLDR.64 D2, [R1, #16]"
    );
}

// ============================================================================
// Coprocessor identification — F64 must use cp11 (0xB), not cp10 (0xA)
// ============================================================================

#[test]
fn test_f64_uses_cp11_thumb() {
    let enc = thumb_encoder();

    // VADD.F64 — cp11
    let bytes = enc
        .encode(&ArmOp::F64Add {
            dd: VfpReg::D0,
            dn: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    let instr = thumb_bytes_to_word(&bytes);
    assert_eq!(
        (instr >> 8) & 0xF,
        0xB,
        "F64Add must use coprocessor 11 (0xB)"
    );

    // VSQRT.F64 — cp11
    let bytes = enc
        .encode(&ArmOp::F64Sqrt {
            dd: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    let instr = thumb_bytes_to_word(&bytes);
    assert_eq!(
        (instr >> 8) & 0xF,
        0xB,
        "F64Sqrt must use coprocessor 11 (0xB)"
    );

    // VLDR.64 — cp11
    let bytes = enc
        .encode(&ArmOp::F64Load {
            dd: VfpReg::D0,
            addr: MemAddr::imm(Reg::R0, 0),
        })
        .unwrap();
    let instr = thumb_bytes_to_word(&bytes);
    assert_eq!(
        (instr >> 8) & 0xF,
        0xB,
        "F64Load must use coprocessor 11 (0xB)"
    );
}

#[test]
fn test_f64_uses_cp11_arm32() {
    let enc = arm_encoder();

    // VADD.F64 — cp11
    let bytes = enc
        .encode(&ArmOp::F64Add {
            dd: VfpReg::D0,
            dn: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();
    let instr = arm_bytes_to_word(&bytes);
    assert_eq!(
        (instr >> 8) & 0xF,
        0xB,
        "F64Add ARM32 must use coprocessor 11 (0xB)"
    );
}

// ============================================================================
// VFP-D bit-pattern symmetry — F64 base matches F32 base + sz bit
// ============================================================================

#[test]
fn test_f64_add_equals_f32_add_with_sz_bit() {
    let enc = thumb_encoder();

    let f32_bytes = enc
        .encode(&ArmOp::F32Add {
            sd: VfpReg::S0,
            sn: VfpReg::S0,
            sm: VfpReg::S0,
        })
        .unwrap();
    let f64_bytes = enc
        .encode(&ArmOp::F64Add {
            dd: VfpReg::D0,
            dn: VfpReg::D0,
            dm: VfpReg::D0,
        })
        .unwrap();

    let f32_word = thumb_bytes_to_word(&f32_bytes);
    let f64_word = thumb_bytes_to_word(&f64_bytes);

    // F32 has sz=0 (cp10), F64 has sz=1 (cp11). Differ only in bit 8.
    assert_eq!(
        f64_word ^ f32_word,
        1 << 8,
        "F32/F64 of the same op should differ only in the sz bit (bit 8)"
    );
}

// ============================================================================
// ARM32 mirrors Thumb-2 (VFP instruction words are identical)
// ============================================================================

#[test]
fn test_vadd_f64_d1_d2_d3_arm32_matches_thumb() {
    let thumb_bytes = thumb_encoder()
        .encode(&ArmOp::F64Add {
            dd: VfpReg::D1,
            dn: VfpReg::D2,
            dm: VfpReg::D3,
        })
        .unwrap();
    let arm_bytes = arm_encoder()
        .encode(&ArmOp::F64Add {
            dd: VfpReg::D1,
            dn: VfpReg::D2,
            dm: VfpReg::D3,
        })
        .unwrap();
    // Both encode the same 32-bit VFP word, just split differently.
    assert_eq!(
        thumb_bytes_to_word(&thumb_bytes),
        arm_bytes_to_word(&arm_bytes)
    );
}

// ============================================================================
// VMOV core ↔ D-register (F64ReinterpretI64 / I64ReinterpretF64)
// ============================================================================

#[test]
fn test_vmov_core_to_dreg_thumb() {
    let enc = thumb_encoder();
    // VMOV D0, R0, R1: 0xEC400B10
    let bytes = enc
        .encode(&ArmOp::F64ReinterpretI64 {
            dd: VfpReg::D0,
            rmlo: Reg::R0,
            rmhi: Reg::R1,
        })
        .unwrap();
    assert_eq!(thumb_bytes_to_word(&bytes), 0xEC410B10, "VMOV D0, R0, R1");
}

#[test]
fn test_vmov_dreg_to_core_thumb() {
    let enc = thumb_encoder();
    // VMOV R0, R1, D0: 0xEC500B10 base, Rt2=R1, Rt=R0, Dm=D0
    // 0xEC500B10 | (1<<16) | (0<<12) | 0 = 0xEC510B10
    let bytes = enc
        .encode(&ArmOp::I64ReinterpretF64 {
            rdlo: Reg::R0,
            rdhi: Reg::R1,
            dm: VfpReg::D0,
        })
        .unwrap();
    assert_eq!(thumb_bytes_to_word(&bytes), 0xEC510B10, "VMOV R0, R1, D0");
}

// ============================================================================
// VCVT.F64.F32 (F64PromoteF32)
// ============================================================================

#[test]
fn test_vcvt_f64_f32_d0_s0_thumb() {
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64PromoteF32 {
            dd: VfpReg::D0,
            sm: VfpReg::S0,
        })
        .unwrap();
    // VCVT.F64.F32 Dd, Sm: 0xEEB70AC0 — all register fields zero.
    assert_eq!(
        thumb_bytes_to_word(&bytes),
        0xEEB70AC0,
        "VCVT.F64.F32 D0, S0"
    );
}

// ============================================================================
// Size & shape — every f64 op the selector emits should encode without panic
// ============================================================================

#[test]
fn test_f64_arith_encodes_to_4_bytes_thumb() {
    let enc = thumb_encoder();
    for op in [
        ArmOp::F64Add {
            dd: VfpReg::D0,
            dn: VfpReg::D1,
            dm: VfpReg::D2,
        },
        ArmOp::F64Sub {
            dd: VfpReg::D3,
            dn: VfpReg::D4,
            dm: VfpReg::D5,
        },
        ArmOp::F64Mul {
            dd: VfpReg::D6,
            dn: VfpReg::D7,
            dm: VfpReg::D8,
        },
        ArmOp::F64Div {
            dd: VfpReg::D9,
            dn: VfpReg::D10,
            dm: VfpReg::D11,
        },
        ArmOp::F64Abs {
            dd: VfpReg::D12,
            dm: VfpReg::D13,
        },
        ArmOp::F64Neg {
            dd: VfpReg::D14,
            dm: VfpReg::D15,
        },
        ArmOp::F64Sqrt {
            dd: VfpReg::D0,
            dm: VfpReg::D1,
        },
    ] {
        let bytes = enc.encode(&op).expect("encoding should succeed");
        assert_eq!(bytes.len(), 4, "{op:?} should encode to 4 bytes");
        // Coprocessor must be cp11
        let word = thumb_bytes_to_word(&bytes);
        assert_eq!(
            (word >> 8) & 0xF,
            0xB,
            "{op:?} must use cp11 (got cp{:X})",
            (word >> 8) & 0xF
        );
    }
}

#[test]
fn test_f64_ldst_encodes_to_4_bytes_thumb() {
    let enc = thumb_encoder();
    for op in [
        ArmOp::F64Load {
            dd: VfpReg::D0,
            addr: MemAddr::imm(Reg::R0, 0),
        },
        ArmOp::F64Load {
            dd: VfpReg::D15,
            addr: MemAddr::imm(Reg::R11, 32),
        },
        ArmOp::F64Store {
            dd: VfpReg::D0,
            addr: MemAddr::imm(Reg::SP, 0),
        },
        ArmOp::F64Store {
            dd: VfpReg::D7,
            addr: MemAddr::imm(Reg::R4, 64),
        },
    ] {
        let bytes = enc.encode(&op).expect("encoding should succeed");
        assert_eq!(bytes.len(), 4, "{op:?} should encode to 4 bytes");
        let word = thumb_bytes_to_word(&bytes);
        assert_eq!((word >> 8) & 0xF, 0xB, "{op:?} must use cp11");
    }
}

#[test]
fn test_f64_const_encodes_to_20_bytes_thumb() {
    // F64Const is a pseudo-op that expands to MOVW+MOVT+MOVW+MOVT+VMOV = 20 bytes
    let enc = thumb_encoder();
    let bytes = enc
        .encode(&ArmOp::F64Const {
            dd: VfpReg::D0,
            value: 1.5,
        })
        .unwrap();
    assert_eq!(bytes.len(), 20, "F64Const should encode to 20 bytes");
}