cbvm/asm/
mod.rs

1//take a ByteStream and turn into asm, run through the stream and emit the asm, every FuncType is a new line, and thrn the ops and then args
2use crate::bytecode::types::Types::{*};
3use crate::builder::bytes::ByteStream;
4use crate::bytecode::ops::Operations::{self, *};
5//import stream macro
6use crate::{stream, byte, typed, Byte, Types};
7
8pub fn mkasm(stream: ByteStream) -> String {
9    println!("{:?}", stream.bytes.len());
10    let mut asm = String::new();
11    for byte in stream.bytes {
12        match byte.tp {
13            TypeOp => {
14                asm.push_str(&format!("\n{:?} ", Operations::from(*(byte.data) as u8)));
15            },
16            TypeReg => {
17                asm.push_str(&format!("[{:x}] ", *(byte.data)));
18            },
19            TypeU64 => {
20                asm.push_str(&format!("64u{:x} ", *(byte.data)));
21            },
22            TypeU8 => {
23                asm.push_str(&format!("8u{:x} ", *(byte.data)));
24            }
25            TypeFunc => {
26                asm.push_str(&format!(":{:?} ", byte.data));
27            },
28            TypeAddr => {
29                asm.push_str(&format!("@{:x} ", *(byte.data)));
30            },
31            TypeI64 => {
32                asm.push_str(&format!("64i{:x} ", *(byte.data)));
33            },
34            TypeI128 => {
35                asm.push_str(&format!("128i{:x} ", *(byte.data)));
36            },
37            TypeU128 => {
38                asm.push_str(&format!("128u{:x} ", *(byte.data)));
39            },
40            TypeF32 => {
41                asm.push_str(&format!("32f{:x} ", *(byte.data)));
42            },
43            TypeF64 => {
44                asm.push_str(&format!("64f{:x} ", *(byte.data)));
45            },
46            DerefStack => {
47                asm.push_str(&format!("({:x}) ", *(byte.data)));
48            },
49            DerefHeapReg => {
50                asm.push_str(&format!("h{:x}", *(byte.data)));
51            },
52            DerefStackReg => {
53                asm.push_str(&format!("s{:x} ", *(byte.data)));
54            },
55            NoType => {
56                asm.push_str(&format!("{:x} ", *(byte.data)));
57            }
58            TypeI8 => {
59                asm.push_str(&format!("{:x} ", *(byte.data)));
60            }
61            TypeJmp => {
62                asm.push_str(&format!("j{:x} ", *(byte.data)));
63            }
64        }
65    }
66    asm
67}
68//function to reverse mkasm
69pub fn rvasm(asm: String) -> ByteStream {
70    let mut stream = ByteStream::new();
71    let mut data = Vec::new();
72    for c in asm.chars() {
73        data.push(c as u8);
74    }
75    let mut pos = 0;
76    while pos < data.len() {
77        let tp = Types::from(data[pos]);
78        pos += 1;
79        let arg = data[pos] as u64;
80        pos += 1;
81        let byte = Byte{
82            data: Box::new(arg),
83            pos: 0,
84            tp: tp,
85        };
86        stream.emit(byte);
87    }
88    stream
89}