write_x86_64 0.2.0

Crate to help you write x86_64 assembly code
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
//! Module to help people generate x86_64 code in Rust
//!
//! Inspired from <https://www.lri.fr/~filliatr/ens/compil/lib/x86_64.ml.html>
//!
//! This crate wants to be an equivalent of the above code
//!
//! For writting files see
//!
//! [`data::Data`], [`file::File`] and [Text] data structures
//!
//! Registers %rax -> %r15 are all accessible for 8, 16, 32 and 64 bits.
//! Write name in capital letters to access them
//!
//! Operands can be obtained with [`lab`], [`ilab`], [`addr`], [`reg!`] and [`immb`] to [`immq`].
//!
//! All instruction are available for various sizes.
//!
//! Transfert instruction : [`movq`]
//!
//! Arithmetic : [`leaq`], [`incq`], [`decq`], [`negq`], [`addq`], [`subq`], [`imulq`], [`cqto`], [`idivq`], [`divq`]
//!
//! Logic : [`notq`], [`andq`], [`orq`], [`xorq`]
//!
//! Shifts : [`shlq`], [`shrq`], [`sarq`]
//!
//! Jumps : [`call`], [`call_star`], [`leave`], [`ret`], [`jmp`], [`jmp_star`], [`jcc`]
//!
//! Conditions : [`cmpb`], [`testq`], [`set`], [`cmovq`]
//!
//! Stack : [`pushq`], [`popq`]
//!
//! Various others : [`label`], [`comment`]

// Author :
// 2022 Samuel VIVIEN

#![warn(missing_docs)]

/// Define data segment
pub mod data;

/// Define structure for whole file
pub mod file;

/// Defines instructions
pub mod instr;

/// Defines registers and operands
pub mod reg;

/// Defines various traits
pub mod traits;

/// Defines debug symbols
pub mod debug;

/// Defines directives
pub mod directives;

#[macro_use]
mod macros;

#[cfg(test)]
mod tests;

use std::io::prelude::*;
use std::ops::{Add, AddAssign};

// Segments

/// Structure representing a segment element
pub enum SegmentEL<T> {
    /// Label
    Label(reg::Label),

    /// Inlining
    Inline(String),

    /// Comment
    Comment(String),

    /// Element
    Data(T),

    /// Directives
    Directive(directives::Directive),
}

impl<T: traits::Writable> traits::Writable for SegmentEL<T> {
    fn write_in(&self, file: &mut std::fs::File) -> std::io::Result<()> {
        match self {
            Self::Label(lab) => {
                lab.write_in(file)?;
                file.write_all(b":")
            }
            Self::Inline(str) => file.write_all(str.as_bytes()),
            Self::Comment(str) => {
                file.write_all(b"## ")?;
                file.write_all(str.as_bytes())
            }
            Self::Data(el) => {
                file.write_all(b"\t")?;
                el.write_in(file)
            }
            Self::Directive(d) => {
                file.write_all(b"\t")?;
                d.write_in(file)
            }
        }
    }
}

impl<T> SegmentEL<T> {
    /// Toward a wrapper for potential comment to the segment element
    pub fn wrapped(self) -> SegmentELWrapper<T> {
        SegmentELWrapper {
            el: self,
            comment: None,
        }
    }
}

/// Wrapper around a segment element to store a potential comment
pub struct SegmentELWrapper<T> {
    el: SegmentEL<T>,
    comment: Option<String>,
}

impl<T> SegmentELWrapper<T> {
    /// Add a comment to segment element
    pub fn comment(&mut self, str: String) {
        match &mut self.comment {
            None => self.comment = Some(str),
            Some(c) => {
                c.push_str(" ## ");
                c.push_str(&str);
            }
        }
    }
}

impl<T: traits::Writable> traits::Writable for SegmentELWrapper<T> {
    fn write_in(&self, file: &mut std::fs::File) -> std::io::Result<()> {
        self.el.write_in(file)?;
        if let Some(str) = &self.comment {
            file.write_all(b" ## ")?;
            file.write_all(str.as_bytes())?;
        };
        file.write_all(b"\n")
    }
}

/// Segment
pub struct Segment<T> {
    data: Vec<SegmentELWrapper<T>>,
}

impl<T> Segment<T> {
    /// Create a segment with only an element
    fn new(el: T) -> Self {
        Self {
            data: vec![SegmentEL::Data(el).wrapped()],
        }
    }

    /// Create a segment with only a label
    pub fn label(lab: reg::Label) -> Self {
        Self {
            data: vec![SegmentEL::Label(lab).wrapped()],
        }
    }

    /// Create a segment with only something inlined
    pub fn inline(str: String) -> Self {
        Self {
            data: vec![SegmentEL::Inline(str).wrapped()],
        }
    }

    /// Create a segment with only a comment
    pub fn comment(str: String) -> Self {
        Self {
            data: vec![SegmentEL::Comment(str).wrapped()],
        }
    }

    /// Create a segment with only a comment
    pub fn add_comment(mut self, str: String) -> Self {
        match self.data.last_mut() {
            None => self.data.push(SegmentEL::Comment(str).wrapped()),
            Some(el) => el.comment(str),
        }
        self
    }

    /// Add a directive to segment
    pub fn directive(directive: directives::Directive) -> Self {
        Self {
            data: vec![SegmentEL::Directive(directive).wrapped()],
        }
    }

    /// Create a named segment from Segment
    pub fn name(self, name: String) -> NamedSegment<T> {
        NamedSegment::new(name, self)
    }

    /// Create an empty segment
    pub fn empty() -> Self {
        Self { data: Vec::new() }
    }
}

impl<T> Add for Segment<T> {
    type Output = Self;

    fn add(mut self, mut other: Self) -> Self {
        self.data.append(&mut other.data);
        self
    }
}

impl<T> AddAssign for Segment<T> {
    fn add_assign(&mut self, mut rhs: Self) {
        self.data.append(&mut rhs.data)
    }
}

impl<T: traits::Writable> traits::Writable for Segment<T> {
    fn write_in(&self, file: &mut std::fs::File) -> std::io::Result<()> {
        for el in &self.data {
            el.write_in(file)?;
        }
        std::io::Result::Ok(())
    }
}

/// Named Segment
pub struct NamedSegment<T> {
    name: String,
    data: Segment<T>,
}

impl<T> NamedSegment<T> {
    /// Create a named segment from a Segment
    pub fn new(name: String, data: Segment<T>) -> Self {
        Self { name, data }
    }
}

impl<T: traits::Writable> traits::Writable for NamedSegment<T> {
    fn write_in(&self, file: &mut std::fs::File) -> std::io::Result<()> {
        file.write_all(b"\t.section ")?;
        file.write_all(self.name.as_bytes())?;
        file.write_all(b"\n")?;
        self.data.write_in(file)
    }
}

/// Type alias representing the text Segment
pub type Text = Segment<instr::Instr>;

/// Type alias representing the data Segment
pub type Data = Segment<data::DataEL>;

/// nop instruction (does nothing)
pub fn nop() -> Text {
    Segment::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::Nop,
        reg1: None,
        reg2: None,
    }))
}

/// Directly inline assembly code
pub fn inline(str: String) -> Text {
    Text::inline(str)
}

//// Registers

def_regq!(RAX, Rax);
def_regq!(RBX, Rbx);
def_regq!(RCX, Rcx);
def_regq!(RDX, Rdx);
def_regq!(RSI, Rsi);
def_regq!(RDI, Rdi);
def_regq!(RBP, Rbp);
def_regq!(RSP, Rsp);
def_regq!(R8, R8);
def_regq!(R9, R9);
def_regq!(R10, R10);
def_regq!(R11, R11);
def_regq!(R12, R12);
def_regq!(R13, R13);
def_regq!(R14, R14);
def_regq!(R15, R15);

def_regl!(EAX, Eax);
def_regl!(EBX, Ebx);
def_regl!(ECX, Ecx);
def_regl!(EDX, Edx);
def_regl!(ESI, Esi);
def_regl!(EDI, Edi);
def_regl!(EBP, Ebp);
def_regl!(ESP, Esp);
def_regl!(R8D, R8d);
def_regl!(R9D, R9d);
def_regl!(R10D, R10d);
def_regl!(R11D, R11d);
def_regl!(R12D, R12d);
def_regl!(R13D, R13d);
def_regl!(R14D, R14d);
def_regl!(R15D, R15d);

def_regw!(AX, Ax);
def_regw!(BX, Bx);
def_regw!(CX, Cx);
def_regw!(DX, Dx);
def_regw!(SI, Si);
def_regw!(DI, Di);
def_regw!(BP, Bp);
def_regw!(SP, Sp);
def_regw!(R8W, R8w);
def_regw!(R9W, R9w);
def_regw!(R10W, R10w);
def_regw!(R11W, R11w);
def_regw!(R12W, R12w);
def_regw!(R13W, R13w);
def_regw!(R14W, R14w);
def_regw!(R15W, R15w);

def_regb!(AL, Al);
def_regb!(BL, Bl);
def_regb!(CL, Cl);
def_regb!(DL, Dl);
def_regb!(AH, Ah);
def_regb!(BH, Bh);
def_regb!(CH, Ch);
def_regb!(DH, Dh);
def_regb!(SIL, Sil);
def_regb!(DIL, Dil);
def_regb!(BPL, Bpl);
def_regb!(SPL, Spl);
def_regb!(R8B, R8b);
def_regb!(R9B, R9b);
def_regb!(R10B, R10b);
def_regb!(R11B, R11b);
def_regb!(R12B, R12b);
def_regb!(R13B, R13b);
def_regb!(R14B, R14b);
def_regb!(R15B, R15b);

/// Operands

/// Immediate operand for 64-bits instructions
pub fn immq(imm: i64) -> reg::Operand<reg::RegQ> {
    reg::Operand::Imm(imm)
}

/// Immediate operand for 32-bits instructions
pub fn imml(imm: i32) -> reg::Operand<reg::RegL> {
    reg::Operand::Imm(imm as i64)
}

/// Immediate operand for 16-bits instructions
pub fn immw(imm: i16) -> reg::Operand<reg::RegW> {
    reg::Operand::Imm(imm as i64)
}

/// Immediate operand for 8-bits instructions
pub fn immb(imm: i8) -> reg::Operand<reg::RegB> {
    reg::Operand::Imm(imm as i64)
}

/// Macro to convert an element of type R with the trait Reg
/// to an element of type Operand<R>
#[macro_export]
macro_rules! reg {
    ($reg:expr) => {
        $crate::reg::Operand::Reg($reg)
    };
}

/// Create an Operand<R> (for any type R) to access memory
///
/// addr!(rsp) => (%rsp)
///
/// addr!(offset, rbp) => offset(%rbp)
///
/// addr!(offset, rbp, rax) => offset(%rbp, %rax, 1)
///
/// addr!(offset, rbp, rax, scale) => offset(%rbp, %rax, scale)
#[macro_export]
macro_rules! addr {
    ($reg:expr) => {
        $crate::reg::Operand::Addr(0, $reg, None, 0)
    };
    ($offset:expr, $reg:expr) => {
        $crate::reg::Operand::Addr($offset, $reg, None, 0)
    };
    ($offset:expr, $reg:expr, $reg2:expr) => {
        $crate::reg::Operand::Addr($offset, $reg, Some($reg2), 1)
    };
    ($offset:expr, $reg:expr, $reg2:expr, $scale:expr) => {
        $crate::reg::Operand::Addr($offset, $reg, Some($reg2), scale)
    };
}

#[cfg(target_os = "macos")]
#[macro_export]
/// lab operator from <https://www.lri.fr/~filliatr/ens/compil/lib/x86_64.ml.html>
macro_rules! lab {
    ($label:expr) => {
        $crate::reg::Operand::LabRelAddr($label)
    };
}

#[cfg(target_os = "linux")]
#[macro_export]
/// lab operator from <https://www.lri.fr/~filliatr/ens/compil/lib/x86_64.ml.html>
macro_rules! lab {
    ($label:expr) => {
        $crate::reg::Operand::LabAbsAddr($label)
    };
}

#[macro_export]
/// ilab operator from <https://www.lri.fr/~filliatr/ens/compil/lib/x86_64.ml.html>
macro_rules! ilab {
    ($label:expr) => {
        $crate::reg::Operand::LabVal($label)
    };
}

//// Instructions

// Data transfer

build_instr_op_op!(Move, movb, movw, movl, movq);

/// Sign extend for 1-byte to 2-bytes
pub fn movsbw(reg1: reg::Operand<reg::RegB>, reg2: reg::RegW) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Sign extend for 1-byte to 4-bytes
pub fn movsbl(reg1: reg::Operand<reg::RegB>, reg2: reg::RegL) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Sign extend for 1-byte to 8-bytes
pub fn movsbq(reg1: reg::Operand<reg::RegB>, reg2: reg::RegQ) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Sign extend for 2-byte to 4-bytes
pub fn movswl(reg1: reg::Operand<reg::RegW>, reg2: reg::RegL) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Sign extend for 2-byte to 8-bytes
pub fn movswq(reg1: reg::Operand<reg::RegW>, reg2: reg::RegQ) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Sign extend for 4-byte to 8-bytes
pub fn movslq(reg1: reg::Operand<reg::RegL>, reg2: reg::RegQ) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Extension with zeros for 1-byte to 2-bytes
pub fn movzbw(reg1: reg::Operand<reg::RegB>, reg2: reg::RegW) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Extension with zeros for 1-byte to 4-bytes
pub fn movzbl(reg1: reg::Operand<reg::RegB>, reg2: reg::RegL) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Extension with zeros for 1-byte to 8-bytes
pub fn movzbq(reg1: reg::Operand<reg::RegB>, reg2: reg::RegQ) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Extension with zeros for 2-byte to 4-bytes
pub fn movzwl(reg1: reg::Operand<reg::RegW>, reg2: reg::RegL) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

/// Extension with zeros for 2-byte to 8-bytes
pub fn movzwq(reg1: reg::Operand<reg::RegW>, reg2: reg::RegQ) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Movs,
        reg1: Some(reg1),
        reg2: Some(reg!(reg2)),
    }))
}

// Move between different sizes not implemented

// movabsq not implemented

//// Arithmetic

build_instr_op_reg!(Lea, leab, leaw, leal, leaq);

build_instr_op!(Inc, incb, incw, incl, incq);

build_instr_op!(Dec, decb, decw, decl, decq);

build_instr_op!(Neg, negb, negw, negl, negq);

build_instr_op_op!(Add, addb, addw, addl, addq);

build_instr_op_op!(Sub, subb, subw, subl, subq);

build_instr_op_op!(IMul, imulw, imull, imulq);

/// sign extend EAX into EDX::EAX
pub fn cltd() -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::Cltd,
        reg1: None,
        reg2: None,
    }))
}

/// sign extend RAX into RDX::RAX
pub fn cqto() -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::Cqto,
        reg1: None,
        reg2: None,
    }))
}

build_instr_op!(SignedDiv, idivl, idivq);

build_instr_op!(UnsignedDiv, divl, divq);

///// Logic operations
// Those operations are bitwise operations

build_instr_op!(Not, notb, notw, notl, notq);

build_instr_op_op!(And, andb, andw, andl, andq);

build_instr_op_op!(Or, orb, orw, orl, orq);

build_instr_op_op!(Xor, xorb, xorw, xorl, xorq);

//// Shifts

build_instr_op_op!(Shl, shlb, shlw, shll, shlq);
build_instr_op_op!(Shr, shrb, shrw, shrl, shrq);
build_instr_op_op!(Sar, sarb, sarw, sarl, sarq);

/// logical shift of register by value in CL
pub fn shlb_reg(reg: reg::Operand<reg::RegB>) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Shl,
        reg1: Some(reg!(CL)),
        reg2: Some(reg),
    }))
}

/// logical shift of register by value in CL
pub fn shlw_reg(reg: reg::Operand<reg::RegW>) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Shl,
        reg1: Some(reg!(CL)),
        reg2: Some(reg),
    }))
}

/// logical shift of register by value in CL
pub fn shll_reg(reg: reg::Operand<reg::RegL>) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Shl,
        reg1: Some(reg!(CL)),
        reg2: Some(reg),
    }))
}

/// logical shift of register by value in CL
pub fn shlq_reg(reg: reg::Operand<reg::RegQ>) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Shl,
        reg1: Some(reg!(CL)),
        reg2: Some(reg),
    }))
}

/// logical shift of register by value in CL
pub fn shrb_reg(reg: reg::Operand<reg::RegB>) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Shr,
        reg1: Some(reg!(CL)),
        reg2: Some(reg),
    }))
}

/// logical shift of register by value in CL
pub fn shrw_reg(reg: reg::Operand<reg::RegW>) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Shr,
        reg1: Some(reg!(CL)),
        reg2: Some(reg),
    }))
}

/// logical shift of register by value in CL
pub fn shrl_reg(reg: reg::Operand<reg::RegL>) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Shr,
        reg1: Some(reg!(CL)),
        reg2: Some(reg),
    }))
}

/// logical shift of register by value in CL
pub fn shrq_reg(reg: reg::Operand<reg::RegQ>) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Shr,
        reg1: Some(reg!(CL)),
        reg2: Some(reg),
    }))
}

//// Jumps

// Function calls and return

/// Call label
pub fn call(label: reg::Label) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::Call(label),
        reg1: None,
        reg2: None,
    }))
}

/// Call address
pub fn call_star(op: reg::Operand<reg::RegQ>) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegQ, reg::RegInv> {
        instr: instr::InstrName::CallStar,
        reg1: Some(op),
        reg2: None,
    }))
}

/// Leave instruction
pub fn leave() -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::Leave,
        reg1: None,
        reg2: None,
    }))
}

/// Equivalent to popq %rip
pub fn ret() -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::Ret,
        reg1: None,
        reg2: None,
    }))
}

/// Jump to label
pub fn jmp(label: reg::Label) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::Jump(label),
        reg1: None,
        reg2: None,
    }))
}

/// Jump to address
pub fn jmp_star(op: reg::Operand<reg::RegQ>) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegQ, reg::RegInv> {
        instr: instr::InstrName::JumpStar,
        reg1: Some(op),
        reg2: None,
    }))
}

////// Conditional jumps

/// Conditional jump
pub fn jcc(cond: instr::Cond, label: reg::Label) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::CondJump(cond, label),
        reg1: None,
        reg2: None,
    }))
}

/// Conditional jump if zero
pub fn jz(label: reg::Label) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::CondJump(instr::Cond::Z, label),
        reg1: None,
        reg2: None,
    }))
}

/// Conditional jump if not zero
pub fn jnz(label: reg::Label) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::CondJump(instr::Cond::NZ, label),
        reg1: None,
        reg2: None,
    }))
}

/// Conditional jump if above equal
pub fn jae(label: reg::Label) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegInv, reg::RegInv> {
        instr: instr::InstrName::CondJump(instr::Cond::AE, label),
        reg1: None,
        reg2: None,
    }))
}

//// Conditions

build_instr_op_op!(Cmp, cmpb, cmpw, cmpl, cmpq);

build_instr_op_op!(Test, testb, testw, testl, testq);

/// Conditionnal set
pub fn set(cond: instr::Cond, reg: reg::Operand<reg::RegB>) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegB, reg::RegInv> {
        instr: instr::InstrName::Set(cond),
        reg1: Some(reg),
        reg2: None,
    }))
}

//// Stack handling

/// Push 8-bytes on stack
pub fn pushq(op: reg::Operand<reg::RegQ>) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegQ, reg::RegInv> {
        instr: instr::InstrName::Push,
        reg1: Some(op),
        reg2: None,
    }))
}

/// Pop 8-bytes from stack
pub fn popq(reg: reg::RegQ) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegQ, reg::RegInv> {
        instr: instr::InstrName::Pop,
        reg1: Some(reg!(reg)),
        reg2: None,
    }))
}

//// Various others

/// Add comment to Assembly (should not contain de line break!)
pub fn comment(s: String) -> Text {
    Text::comment(s)
}

#[cfg(target_os = "linux")]
/// Move address of label in register (implementation is OS dependant)
///
/// Usefull to get address to string before calling printf
///
/// Not sure it works on linux, needs to test!
pub fn deplq(l: reg::Label, reg: reg::RegQ) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegQ, reg::RegQ> {
        instr: instr::InstrName::Move,
        reg1: Some(reg::Operand::LabAbsAddr(l)),
        reg2: Some(reg!(reg)),
    }))
}

#[cfg(target_os = "macos")]
/// Move address of label in register (implementation is OS dependant)
///
/// Usefull to get address to string before calling printf
///
/// Not sure it works on linux, needs to test!
pub fn deplq(l: reg::Label, reg: reg::RegQ) -> Text {
    Text::new(Box::new(instr::Instruction::<reg::RegQ, reg::RegQ> {
        instr: instr::InstrName::Lea,
        reg1: Some(reg::Operand::LabAbsAddr(l)),
        reg2: Some(reg!(reg)),
    }))
}

// cmovb is not valid

/// Conditional move of 2-bytes operands
pub fn cmovw(
    cond: instr::Cond,
    reg1: reg::Operand<reg::RegW>,
    reg2: reg::Operand<reg::RegW>,
) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Cmov(cond),
        reg1: Some(reg1),
        reg2: Some(reg2),
    }))
}

/// Conditional move of 4-bytes operands
pub fn cmovl(
    cond: instr::Cond,
    reg1: reg::Operand<reg::RegL>,
    reg2: reg::Operand<reg::RegL>,
) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Cmov(cond),
        reg1: Some(reg1),
        reg2: Some(reg2),
    }))
}

/// Conditional move of 8-bytes operands
pub fn cmovq(
    cond: instr::Cond,
    reg1: reg::Operand<reg::RegQ>,
    reg2: reg::Operand<reg::RegQ>,
) -> Text {
    Text::new(Box::new(instr::Instruction {
        instr: instr::InstrName::Cmov(cond),
        reg1: Some(reg1),
        reg2: Some(reg2),
    }))
}

/// Convert str to label name
pub fn new_label(name: &str) -> reg::Label {
    reg::Label::from_str(name.to_string())
}