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
use std::{collections::HashMap, sync::Arc};

use crate::{
    asm_generation::{
        asm_builder::{AsmBuilder, AsmBuilderResult},
        from_ir::StateAccessType,
        ProgramKind,
    },
    asm_lang::Label,
    error::*,
    metadata::MetadataManager,
};
use etk_ops::london::*;
use sway_error::error::CompileError;
use sway_ir::{Context, *};
use sway_types::Span;

use etk_asm::{asm::Assembler, ops::*};

/// A smart contract is created by sending a transaction with an empty "to" field.
/// When this is done, the Ethereum virtual machine (EVM) runs the bytecode which is
/// set in the init byte array which is a field that can contain EVM bytecode
///
/// The EVM bytecode that is then stored on the blockchain is the value that is
/// returned by running the content of init on the EVM.
///
/// The bytecode can refer to itself through the opcode CODECOPY opcode, which reads
/// three values on the stack where two of those values are pointers to the bytecode,
/// one marking the beginning and one marking the end of what should be copied to memory.
///
/// The RETURN opcode is then used, along with the correct values placed on the stack,
/// to return bytecode from the initial run of the EVM code.
/// RETURN reads and removes two pointers from the stack.
/// These pointers define the part of the memory that is a return value.
/// The return value of the initial contract creating run of the bytecode defines
/// the bytecode that is stored on the blockchain and associated with the address
/// on which you have created the smart contract.
///
/// The code that is compiled but not stored on the blockchain is thus the code needed
/// to store the correct code on the blockchain but also any logic that is contained in
/// a (potential) constructor of the contract.

pub struct EvmAsmBuilder<'ir> {
    #[allow(dead_code)]
    program_kind: ProgramKind,

    sections: Vec<EvmAsmSection>,

    // Label maps are from IR functions or blocks to label name.  Functions have a start and end
    // label.
    pub(super) func_label_map: HashMap<Function, (Label, Label)>,
    #[allow(dead_code)]
    pub(super) block_label_map: HashMap<Block, Label>,

    // IR context we're compiling.
    context: &'ir Context,

    // Metadata manager for converting metadata to Spans, etc.
    md_mgr: MetadataManager,

    // Monotonically increasing unique identifier for label generation.
    label_idx: usize,

    // In progress EVM asm section.
    pub(super) cur_section: Option<EvmAsmSection>,
}

#[derive(Default, Debug)]
pub struct EvmAsmSection {
    ops: Vec<etk_asm::ops::AbstractOp>,
    abi: Vec<ethabi::operation::Operation>,
}

impl EvmAsmSection {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn size(&self) -> usize {
        let mut asm = Assembler::new();
        if asm.push_all(self.ops.clone()).is_err() {
            panic!("Could not size EVM assembly section");
        }
        asm.take().len()
    }
}

pub struct EvmAsmBuilderResult {
    pub ops: Vec<etk_asm::ops::AbstractOp>,
    pub ops_runtime: Vec<etk_asm::ops::AbstractOp>,
    pub abi: EvmAbiResult,
}

pub type EvmAbiResult = Vec<ethabi::operation::Operation>;

impl<'ir> AsmBuilder for EvmAsmBuilder<'ir> {
    fn func_to_labels(&mut self, func: &Function) -> (Label, Label) {
        self.func_to_labels(func)
    }

    fn compile_function(&mut self, function: Function) -> CompileResult<()> {
        self.compile_function(function)
    }

    fn finalize(&self) -> AsmBuilderResult {
        self.finalize()
    }
}

#[allow(unused_variables)]
#[allow(dead_code)]
impl<'ir> EvmAsmBuilder<'ir> {
    pub fn new(program_kind: ProgramKind, context: &'ir Context) -> Self {
        Self {
            program_kind,
            sections: Vec::new(),
            func_label_map: HashMap::new(),
            block_label_map: HashMap::new(),
            context,
            md_mgr: MetadataManager::default(),
            label_idx: 0,
            cur_section: None,
        }
    }

    pub fn finalize(&self) -> AsmBuilderResult {
        let mut global_ops = Vec::new();
        let mut global_abi = Vec::new();

        let mut size = 0;
        let mut it = self.sections.iter().peekable();
        while let Some(section) = it.next() {
            size += section.size();
            global_ops.append(&mut section.ops.clone());
            global_abi.append(&mut section.abi.clone());

            if it.peek().is_some() {
                size += AbstractOp::Op(Op::Invalid(etk_ops::london::Invalid))
                    .size()
                    .unwrap();
                global_ops.push(AbstractOp::Op(Op::Invalid(etk_ops::london::Invalid)));
            }
        }

        // First generate a dummy ctor section to calculate its size.
        let dummy = self.generate_constructor(false, size, 0);

        // Generate the actual ctor section with the correct size..
        let mut ctor = self.generate_constructor(false, size, dummy.size());
        ctor.ops.append(&mut global_ops);
        global_abi.append(&mut ctor.abi);

        AsmBuilderResult::Evm(EvmAsmBuilderResult {
            ops: ctor.ops.clone(),
            ops_runtime: ctor.ops,
            abi: global_abi,
        })
    }

    fn generate_constructor(
        &self,
        is_payable: bool,
        data_size: usize,
        data_offset: usize,
    ) -> EvmAsmSection {
        // For more details and explanations see:
        // https://medium.com/@hayeah/diving-into-the-ethereum-vm-part-5-the-smart-contract-creation-process-cb7b6133b855.

        let mut s = EvmAsmSection::new();
        self.setup_free_memory_pointer(&mut s);

        if is_payable {
            // Get the the amount of ETH transferred to the contract by the parent contract,
            // or by a transaction and check for a non-payable contract. Revert if caller
            // sent ether.
            //
            //   callvalue
            //   dup1
            //   iszero
            //   push1 0x0f
            //   jumpi
            //   push1 0x00
            //   dup1
            //   revert
            //   jumpdest
            //   pop

            s.ops.push(AbstractOp::new(Op::CallValue(CallValue)));
            s.ops.push(AbstractOp::new(Op::Dup1(Dup1)));
            s.ops.push(AbstractOp::new(Op::IsZero(IsZero)));
            let tag_label = "tag_1";
            s.ops.push(AbstractOp::new(Op::Push1(Push1(Imm::with_label(
                tag_label,
            )))));
            s.ops.push(AbstractOp::new(Op::JumpI(JumpI)));
            s.ops
                .push(AbstractOp::new(Op::Push1(Push1(Imm::with_expression(
                    Expression::Terminal(0x00.into()),
                )))));
            s.ops.push(AbstractOp::new(Op::Dup1(Dup1)));
            s.ops.push(AbstractOp::new(Op::Revert(Revert)));

            s.ops.push(AbstractOp::Label("tag_1".into()));
            s.ops.push(AbstractOp::new(Op::JumpDest(JumpDest)));
            s.ops.push(AbstractOp::Op(Op::Pop(Pop)));
        }

        self.copy_contract_code_to_memory(&mut s, data_size, data_offset);

        s.abi.push(ethabi::operation::Operation::Constructor(
            ethabi::Constructor { inputs: vec![] },
        ));

        s
    }

    fn copy_contract_code_to_memory(
        &self,
        s: &mut EvmAsmSection,
        data_size: usize,
        data_offset: usize,
    ) {
        // Copy contract code into memory, and return.
        //   push1 dataSize
        //   dup1
        //   push1 dataOffset
        //   push1 0x00
        //   codecopy
        //   push1 0x00
        //   return
        s.ops.push(AbstractOp::Push(Imm::from(Terminal::Number(
            data_size.into(),
        ))));
        s.ops.push(AbstractOp::new(Op::Dup1(Dup1)));
        s.ops.push(AbstractOp::Push(Imm::from(Terminal::Number(
            data_offset.into(),
        ))));
        s.ops
            .push(AbstractOp::new(Op::Push1(Push1(Imm::with_expression(
                Expression::Terminal(0x00.into()),
            )))));
        s.ops.push(AbstractOp::Op(Op::CodeCopy(CodeCopy)));

        s.ops
            .push(AbstractOp::new(Op::Push1(Push1(Imm::with_expression(
                Expression::Terminal(0x00.into()),
            )))));
        s.ops.push(AbstractOp::Op(Op::Return(Return)));
    }

    fn setup_free_memory_pointer(&self, s: &mut EvmAsmSection) {
        // Setup the initial free memory pointer.
        //
        // The "free memory pointer" is stored at position 0x40 in memory.
        // The first 64 bytes of memory can be used as "scratch space" for short-term allocation.
        // The 32 bytes after the free memory pointer (i.e., starting at 0x60) are meant to be
        // zero permanently and is used as the initial value for empty dynamic memory arrays.
        // This means that the allocatable memory starts at 0x80, which is the initial value
        // of the free memory pointer.
        //
        //   push1 0x80
        //   push1 0x40
        //   mstore

        s.ops
            .push(AbstractOp::new(Op::Push1(Push1(Imm::with_expression(
                Expression::Terminal(0x80.into()),
            )))));
        s.ops
            .push(AbstractOp::new(Op::Push1(Push1(Imm::with_expression(
                Expression::Terminal(0x40.into()),
            )))));
        s.ops.push(AbstractOp::new(Op::MStore(MStore)));
    }

    fn empty_span() -> Span {
        let msg = "unknown source location";
        Span::new(Arc::from(msg), 0, msg.len(), None).unwrap()
    }

    fn get_label(&mut self) -> Label {
        let next_val = self.label_idx;
        self.label_idx += 1;
        Label(self.label_idx)
    }

    pub(super) fn compile_instruction(
        &mut self,
        instr_val: &Value,
        func_is_entry: bool,
    ) -> CompileResult<()> {
        let mut warnings = Vec::new();
        let mut errors = Vec::new();
        if let Some(instruction) = instr_val.get_instruction(self.context) {
            match instruction {
                Instruction::AsmBlock(asm, args) => {
                    check!(
                        self.compile_asm_block(instr_val, asm, args),
                        return err(warnings, errors),
                        warnings,
                        errors
                    )
                }
                Instruction::BitCast(val, ty) => self.compile_bitcast(instr_val, val, ty),
                Instruction::BinaryOp { op, arg1, arg2 } => {
                    self.compile_binary_op(instr_val, op, arg1, arg2)
                }
                Instruction::Branch(to_block) => self.compile_branch(to_block),
                Instruction::Call(func, args) => self.compile_call(instr_val, func, args),
                Instruction::CastPtr(val, ty) => self.compile_cast_ptr(instr_val, val, ty),
                Instruction::Cmp(pred, lhs_value, rhs_value) => {
                    self.compile_cmp(instr_val, pred, lhs_value, rhs_value)
                }
                Instruction::ConditionalBranch {
                    cond_value,
                    true_block,
                    false_block,
                } => check!(
                    self.compile_conditional_branch(cond_value, true_block, false_block),
                    return err(warnings, errors),
                    warnings,
                    errors
                ),
                Instruction::ContractCall {
                    params,
                    coins,
                    asset_id,
                    gas,
                    ..
                } => self.compile_contract_call(instr_val, params, coins, asset_id, gas),
                Instruction::FuelVm(fuel_vm_instr) => {
                    errors.push(CompileError::Internal(
                        "Invalid FuelVM IR instruction provided to the EVM code gen.",
                        self.md_mgr
                            .val_to_span(self.context, *instr_val)
                            .unwrap_or_else(Self::empty_span),
                    ));
                }
                Instruction::GetElemPtr {
                    base,
                    elem_ptr_ty,
                    indices,
                } => self.compile_get_elem_ptr(instr_val, base, elem_ptr_ty, indices),
                Instruction::GetLocal(local_var) => self.compile_get_local(instr_val, local_var),
                Instruction::IntToPtr(val, _) => self.compile_int_to_ptr(instr_val, val),
                Instruction::Load(src_val) => check!(
                    self.compile_load(instr_val, src_val),
                    return err(warnings, errors),
                    warnings,
                    errors
                ),
                Instruction::MemCopyBytes {
                    dst_val_ptr,
                    src_val_ptr,
                    byte_len,
                } => self.compile_mem_copy_bytes(instr_val, dst_val_ptr, src_val_ptr, *byte_len),
                Instruction::MemCopyVal {
                    dst_val_ptr,
                    src_val_ptr,
                } => self.compile_mem_copy_val(instr_val, dst_val_ptr, src_val_ptr),
                Instruction::Nop => (),
                Instruction::PtrToInt(ptr_val, int_ty) => {
                    self.compile_ptr_to_int(instr_val, ptr_val, int_ty)
                }
                Instruction::Ret(ret_val, ty) => {
                    if func_is_entry {
                        self.compile_ret_from_entry(instr_val, ret_val, ty)
                    } else {
                        self.compile_ret_from_call(instr_val, ret_val)
                    }
                }
                Instruction::Store {
                    dst_val_ptr: dst_val,
                    stored_val,
                } => check!(
                    self.compile_store(instr_val, dst_val, stored_val),
                    return err(warnings, errors),
                    warnings,
                    errors
                ),
            }
        } else {
            errors.push(CompileError::Internal(
                "Value not an instruction.",
                self.md_mgr
                    .val_to_span(self.context, *instr_val)
                    .unwrap_or_else(Self::empty_span),
            ));
        }
        ok((), warnings, errors)
    }

    fn compile_asm_block(
        &mut self,
        instr_val: &Value,
        asm: &AsmBlock,
        asm_args: &[AsmArg],
    ) -> CompileResult<()> {
        todo!();
    }

    fn compile_bitcast(&mut self, instr_val: &Value, bitcast_val: &Value, to_type: &Type) {
        todo!();
    }

    fn compile_binary_op(
        &mut self,
        instr_val: &Value,
        op: &BinaryOpKind,
        arg1: &Value,
        arg2: &Value,
    ) {
        todo!();
    }

    fn compile_branch(&mut self, to_block: &BranchToWithArgs) {
        todo!();
    }

    fn compile_cast_ptr(&mut self, instr_val: &Value, val: &Value, ty: &Type) {
        todo!();
    }

    fn compile_cmp(
        &mut self,
        instr_val: &Value,
        pred: &Predicate,
        lhs_value: &Value,
        rhs_value: &Value,
    ) {
        todo!();
    }

    fn compile_conditional_branch(
        &mut self,
        cond_value: &Value,
        true_block: &BranchToWithArgs,
        false_block: &BranchToWithArgs,
    ) -> CompileResult<()> {
        todo!();
    }

    fn compile_branch_to_phi_value(&mut self, to_block: &BranchToWithArgs) {
        todo!();
    }

    #[allow(clippy::too_many_arguments)]
    fn compile_contract_call(
        &mut self,
        instr_val: &Value,
        params: &Value,
        coins: &Value,
        asset_id: &Value,
        gas: &Value,
    ) {
        todo!();
    }

    fn compile_get_storage_key(&mut self, instr_val: &Value) -> CompileResult<()> {
        todo!();
    }

    fn compile_get_elem_ptr(
        &mut self,
        instr_val: &Value,
        base: &Value,
        elem_ptr_ty: &Type,
        indices: &[Value],
    ) {
        todo!();
    }

    fn compile_get_local(&mut self, instr_val: &Value, local_var: &LocalVar) {
        todo!();
    }

    fn compile_gtf(&mut self, instr_val: &Value, index: &Value, tx_field_id: u64) {
        todo!();
    }

    fn compile_int_to_ptr(&mut self, instr_val: &Value, int_to_ptr_val: &Value) {
        todo!();
    }

    fn compile_load(&mut self, instr_val: &Value, src_val: &Value) -> CompileResult<()> {
        todo!();
    }

    fn compile_log(&mut self, instr_val: &Value, log_val: &Value, log_ty: &Type, log_id: &Value) {
        todo!();
    }

    fn compile_mem_copy_bytes(
        &mut self,
        instr_val: &Value,
        dst_val_ptr: &Value,
        src_val_ptr: &Value,
        byte_len: u64,
    ) {
        todo!();
    }

    fn compile_mem_copy_val(
        &mut self,
        instr_val: &Value,
        dst_val_ptr: &Value,
        src_val_ptr: &Value,
    ) {
        todo!();
    }

    fn compile_ptr_to_int(&mut self, instr_val: &Value, ptr_val: &Value, int_ty: &Type) {
        todo!();
    }

    fn compile_read_register(&mut self, instr_val: &Value, reg: &sway_ir::Register) {
        todo!();
    }

    fn compile_ret_from_entry(&mut self, instr_val: &Value, ret_val: &Value, ret_type: &Type) {
        if ret_type.is_unit(self.context) {
            // Unit returns should always be zero, although because they can be omitted from
            // functions, the register is sometimes uninitialized. Manually return zero in this
            // case.
            self.cur_section
                .as_mut()
                .unwrap()
                .ops
                .push(AbstractOp::Op(Op::Return(Return)));
        } else {
            todo!();
        }
    }

    fn compile_revert(&mut self, instr_val: &Value, revert_val: &Value) {
        todo!();
    }

    fn compile_smo(
        &mut self,
        instr_val: &Value,
        recipient_and_message: &Value,
        message_size: &Value,
        output_index: &Value,
        coins: &Value,
    ) {
        todo!();
    }

    fn compile_state_access_quad_word(
        &mut self,
        instr_val: &Value,
        val: &Value,
        key: &Value,
        number_of_slots: &Value,
        access_type: StateAccessType,
    ) -> CompileResult<()> {
        todo!();
    }

    fn compile_state_load_word(&mut self, instr_val: &Value, key: &Value) -> CompileResult<()> {
        todo!();
    }

    fn compile_state_store_word(
        &mut self,
        instr_val: &Value,
        store_val: &Value,
        key: &Value,
    ) -> CompileResult<()> {
        todo!();
    }

    fn compile_store(
        &mut self,
        instr_val: &Value,
        dst_val: &Value,
        stored_val: &Value,
    ) -> CompileResult<()> {
        todo!();
    }

    pub(super) fn func_to_labels(&mut self, func: &Function) -> (Label, Label) {
        self.func_label_map.get(func).cloned().unwrap_or_else(|| {
            let labels = (self.get_label(), self.get_label());
            self.func_label_map.insert(*func, labels);
            labels
        })
    }

    pub fn compile_function(&mut self, function: Function) -> CompileResult<()> {
        self.cur_section = Some(EvmAsmSection::new());

        // push1 0x80
        // push1 0x40
        // mstore
        self.cur_section
            .as_mut()
            .unwrap()
            .ops
            .push(AbstractOp::new(Op::Push1(Push1(Imm::with_expression(
                Expression::Terminal(0x80.into()),
            )))));
        self.cur_section
            .as_mut()
            .unwrap()
            .ops
            .push(AbstractOp::new(Op::Push1(Push1(Imm::with_expression(
                Expression::Terminal(0x40.into()),
            )))));
        self.cur_section
            .as_mut()
            .unwrap()
            .ops
            .push(AbstractOp::new(Op::MStore(MStore)));

        //self.init_locals(function);
        let func_is_entry = function.is_entry(self.context);

        // Compile instructions.
        let mut warnings = Vec::new();
        let mut errors = Vec::new();
        for block in function.block_iter(self.context) {
            self.insert_block_label(block);
            for instr_val in block.instruction_iter(self.context) {
                check!(
                    self.compile_instruction(&instr_val, func_is_entry),
                    return err(warnings, errors),
                    warnings,
                    errors
                );
            }
        }

        // push1 0x00
        // dup1
        // revert
        self.cur_section
            .as_mut()
            .unwrap()
            .ops
            .push(AbstractOp::new(Op::Push1(Push1(Imm::with_expression(
                Expression::Terminal(0x00.into()),
            )))));
        self.cur_section
            .as_mut()
            .unwrap()
            .ops
            .push(AbstractOp::new(Op::Dup1(Dup1)));
        self.cur_section
            .as_mut()
            .unwrap()
            .ops
            .push(AbstractOp::new(Op::Revert(Revert)));

        // Generate the ABI.
        #[allow(deprecated)]
        self.cur_section
            .as_mut()
            .unwrap()
            .abi
            .push(ethabi::operation::Operation::Function(ethabi::Function {
                name: function.get_name(self.context).to_string(),
                inputs: vec![],
                outputs: vec![],
                constant: None,
                state_mutability: ethabi::StateMutability::NonPayable,
            }));

        self.sections.push(self.cur_section.take().unwrap());
        self.cur_section = None;

        ok((), vec![], vec![])
    }

    pub(super) fn compile_call(&mut self, instr_val: &Value, function: &Function, args: &[Value]) {
        todo!();
    }

    pub(super) fn compile_ret_from_call(&mut self, instr_val: &Value, ret_val: &Value) {
        todo!();
    }

    pub(super) fn insert_block_label(&mut self, block: Block) {
        if &block.get_label(self.context) != "entry" {
            let label = self.block_to_label(&block);
            self.cur_section
                .as_mut()
                .unwrap()
                .ops
                .push(AbstractOp::Label(label.to_string()));
        }
    }

    fn block_to_label(&mut self, block: &Block) -> Label {
        self.block_label_map.get(block).cloned().unwrap_or_else(|| {
            let label = self.get_label();
            self.block_label_map.insert(*block, label);
            label
        })
    }
}