Skip to content

Commit

Permalink
porting from gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Aug 3, 2024
0 parents commit 81a45ef
Show file tree
Hide file tree
Showing 31 changed files with 2,316 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test [Bulut]

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Build
run: cargo build --release --verbose

- name: Run lint
run: cargo clippy --verbose

- name: Run tests
run: cargo test --verbose
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Cargo
# will have compiled files and executables
target

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

.idea
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use_field_init_shorthand = true
23 changes: 23 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "bulut"
version = "0.3.0"
edition = "2021"
homepage = "https://osmon.dev"
documentation = "https://wiki.osmon.dev"
repository = "https://github.com/osmon-lang/bulut"
description = "Lightweight and fast Virtual Machine built for Osmon Programming Language"
authors = ["Yuri Katsuki <[email protected]>"]
readme = "readme.md"
keywords = ["vm","register","register-based", "uzbek"]
license = "Apache-2.0"
exclude = ["target"]

[profile.dev]

[profile.release]
lto = true

[dependencies]
time = "0.1.40"
libc = "0.2.43"
colored = "1.6.1"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Osmon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions bytecode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "osmon_bytecode"
version = "0.1.0"
authors = ["UwUssimo Robinson <[email protected]>"]
edition = "2018"
description = "Library used for encoding/decoding osmon instructions"
license = "MIT"
keywords = ["codegen","vm","encoding","decoding"]

[dependencies]
bulut = { path = ".." }
48 changes: 48 additions & 0 deletions bytecode/src/assembler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use bulut::opcodes::Instruction;
use crate::opcode::{Opcode,Size};
use crate::encode;

#[derive(Clone,Debug)]
pub struct Assembler {
pub instructions: Vec<Instruction>,
pub code: Vec<u8>,
}

impl Assembler {
pub fn new(code: Vec<Instruction>) -> Assembler {
Assembler {
instructions: code,
code: vec![]
}
}

pub fn translate(&mut self) {
let mut ip = 0;
while ip < self.instructions.len() {
let instruction = self.instructions[ip].clone();

match instruction {
Instruction::LoadInt(reg,val) => {
self.code.push(Opcode::LoadI);
self.code.push(reg as u8);
self.code.extend_from_slice(&encode!(val;i32));
}
Instruction::Move(reg,reg2) => {
self.code.push(Opcode::Move);
self.code.push(reg as u8);
self.code.push(reg2 as u8);
}
Instruction::LoadLong(reg,val) => {
self.code.push(Opcode::LoadL);
self.code.push(reg as u8);
self.code.extend_from_slice(&encode!(val;i64));
}
_ => unimplemented!(),
}

ip += 1;
}
self.code.push(self.instructions.len() as u8);
}
}

26 changes: 26 additions & 0 deletions bytecode/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
extern crate bulut;


pub mod opcode;
pub mod parser;
pub mod assembler;

#[macro_export]
macro_rules! encode {
($v:expr; $t: ty) => {
unsafe {
::std::mem::transmute::<$t,[u8;::std::mem::size_of::<$t>()]>($v)
}
};
}

#[macro_export]
macro_rules! decode {
($arr: expr; $t: ty) => {
unsafe {
::std::mem::transmute::<[u8;::std::mem::size_of::<$t>()],$t>($arr)
}
};
}
21 changes: 21 additions & 0 deletions bytecode/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate osmon_bytecode;
extern crate bulut;
use osmon_bytecode::parser::Parser;
use osmon_bytecode::assembler::Assembler;
use bulut::opcodes::Instruction;

fn main() {
let mut assembler = Assembler::new(vec![
Instruction::LoadInt(1,12),
Instruction::Move(1,2),
]);

assembler.translate();

println!("{:?}",assembler.code);

let mut parser = Parser::new(&assembler.code);
let code = parser.parse();
println!("{:?}",code);

}
36 changes: 36 additions & 0 deletions bytecode/src/opcode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pub mod Opcode {
pub const LoadI: u8 = 0x1;
pub const LoadF: u8 = 0x2;
pub const LoadL: u8 = 0x3;
pub const LoadD: u8 = 0x4;

pub const LoadG: u8 = 0xa1;
pub const LoadAt: u8 = 0xa2;
pub const StoreAt: u8 = 0xa3;
pub const Ret: u8 = 0xa4;
pub const Ret0: u8 = 0xa5;
pub const Call: u8 = 0xa6;
pub const StoreG: u8 = 0xa7;
pub const Move: u8 = 0xa8;

pub const Label: u8 = 0xa9;
pub const Goto: u8 = 0xe1;
pub const GotoT: u8 = 0xe2;
pub const GotoF: u8 = 0xe3;

pub fn to_string<'a>(op: u8) -> &'a str {
match op {
LoadI => "LoadI",
Move => "Move",
_ => "",
}
}
}

pub mod Size {
pub const Float: u32 = ::std::mem::size_of::<f32>() as u32;
pub const Double: u32 = ::std::mem::size_of::<f64>() as u32;
pub const Int: u32 = ::std::mem::size_of::<i32>() as u32;
pub const Long: u32 = ::std::mem::size_of::<i64>() as u32;
pub const Bool: u32 = ::std::mem::size_of::<bool>() as u32;
}
127 changes: 127 additions & 0 deletions bytecode/src/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use bulut::opcodes::Instruction;
use super::opcode::{Opcode,Size};
use super::decode;

#[derive(Clone,Debug)]
pub struct Parser<'a> {
pub code: &'a [u8],
pub parsed_code: Vec<Instruction>,
pub ip: usize,
}


impl<'a> Parser<'a> {
pub fn new(code: &'a [u8]) -> Parser<'a> {
Parser {
code,
parsed_code: vec![],
ip: 0,
}
}

pub fn read_next(&mut self) -> u8 {
if self.ip < self.code.len() {
let op = self.code[self.ip];

self.ip += 1;
op
} else {

0x0
}
}
pub fn parse(&mut self) -> Vec<Instruction> {
let size = *self.code.last().unwrap() as usize;
let mut ip = 0;
while ip < size {
if self.ip >= self.code.len() {
break;
}
self.parse_opcode();
ip += 1;

}

self.parsed_code.clone()
}

pub fn parse_opcode(&mut self) {
let op = &self.read_next();
println!("{:?}",Opcode::to_string(op.clone()));
match op {
&Opcode::Move => {
let r1 = self.read_next();
let r2 = self.read_next();
self.parsed_code.push(Instruction::Move(r1 as usize,r2 as usize));
}

&Opcode::LoadI => {

let register = self.read_next() as usize;

let array = {
let mut array = [0u8;Size::Int as usize];
let mut i = 0;
while i < Size::Int {
let idx = self.read_next();
array[i as usize] = idx;
i += 1;
}
array
};

let int = decode!(array;i32);
self.parsed_code.push(Instruction::LoadInt(register,int));
},

&Opcode::LoadL => {
let register = self.read_next() as usize;

let array = {
let mut array = [0u8;Size::Long as usize];
for i in 0..Size::Long as usize {
array[i] = self.read_next();
}
array
};

let long = decode!(array;i64);
self.parsed_code.push(Instruction::LoadLong(register,long));
}
&Opcode::LoadF => {
let register = self.read_next() as usize;

let array = {
let mut array = [0u8;Size::Float as usize];
for i in 0..Size::Float as usize {
array[i] = self.read_next();
}
array
};

let float = decode!(array;f32);
self.parsed_code.push(Instruction::LoadFloat(register,float));
}
&Opcode::Label => {
let label_id = self.read_next() as usize;

self.parsed_code.push(Instruction::Label(label_id));
}

&Opcode::GotoF => {
let reg = self.read_next();
let lbl_id = self.read_next();

self.parsed_code.push(Instruction::GotoF(reg as usize,lbl_id as usize));
}

&Opcode::Goto => {
let lbl_id = self.read_next();

self.parsed_code.push(Instruction::Goto(lbl_id as usize));
}

_ => {}
}
}
}
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Bulut VM

Osmon Dasturlash Tilining virtual mashinasi. Osmondan keltirilgan buyruq registrlar yordamida dasturni ishga tushurish.
Loading

0 comments on commit 81a45ef

Please sign in to comment.