Skip to content

Commit

Permalink
chore: minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Apr 4, 2024
1 parent 3ccac45 commit 7ee5de3
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 321 deletions.
510 changes: 269 additions & 241 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ inkwell = { version = "*", optional = true, features = ["llvm16-0-no-llvm-linkin
[build-dependencies]
cbindgen = "*"
lalrpop = { version = "*", optional = true }
llvm-sys = { version = "160", optional = true, features = ["prefer-dynamic"] }
llvm-sys = { version = "170", optional = true, features = ["prefer-dynamic"] }

[features]
default = ["use_lalrpop", "llvm_backend"]
Expand Down
4 changes: 4 additions & 0 deletions lib/src/backend/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ impl CodeGenBackend for LLVMBackend {
fn gen_operator(&mut self, operator: &mut OperatorExpression) {
todo!()
}

fn get_bytes<W: Write>(&mut self, w: &mut W) -> io::Result<()> {
todo!()
}
}
109 changes: 60 additions & 49 deletions lib/src/backend/lua/dump.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::prelude::*;
use super::LuaBackendCtx;

use crate::backend::lua::bytecode::LuaCode;
use crate::backend::CompiledCode;
Expand All @@ -13,11 +13,66 @@ const LUAC_DATA: &[u8] = &[0x19, 0x93, 0x0d, 0x0a, 0x1a, 0x0a];
const LUAC_INT: u64 = 0x5678;
const LUAC_NUMBER: f64 = 370.5;

pub fn lua_dump_function<W: Write>(f: &Function, w: &mut W) -> io::Result<()> {
let fun_impl = f.read();
let cc = fun_impl.compiled_code().as_ref().unwrap();
pub fn lua_dump_module<W: Write>(ctx: &LuaBackendCtx, w: &mut W) -> io::Result<()> {
// Lua header
lua_dump_bytes(w, LUA_SIGNATURE.as_bytes())?;
// Lua version, 5.4
lua_dump_byte(w, 5 * 16 + 4)?;
// format, now is zero
lua_dump_byte(w, 0)?;
// data
lua_dump_bytes(w, LUAC_DATA)?;
// size of Lua instruction
lua_dump_byte(w, 4)?;
// size of Lua integer
lua_dump_byte(w, 8)?;
// size of Lua Number
lua_dump_byte(w, 8)?;
// LUAC_INT
lua_dump_bytes(w, &LUAC_INT.to_le_bytes())?;
// LUAC_NUMBER
lua_dump_bytes(w, &LUAC_NUMBER.to_le_bytes())?;

// TODO: size of UpValues in byte, write 0 in temp
lua_dump_byte(w, 0)?;
// TODO: source file name
lua_dump_int(w, 0)?;
// TODO: linedefined
lua_dump_int(w, 0)?;
// TODO: lastlinedefined
lua_dump_int(w, 0)?;
// TODO: numparams
lua_dump_byte(w, 0)?;
// TODO: is_vararg
lua_dump_byte(w, 0)?;
// TODO: maxstacksize
lua_dump_byte(w, 0)?;

// Dump Code

// Dump Constants

// Dump UpValues

// Dump Protos

// Dump Debug

// let fun_impl = f.read();
// let cc = fun_impl.compiled_code().as_ref().unwrap();

// cc.dump(w)

// if let Some(main) = app_read.find_declaration_by_name(&StString::new("main")) {
// let id = main.read().unwrap().id();
// let func = app_read.get_function(id).unwrap();

// let mut buf = vec![0u8; 0];
// lua_dump_module(func, &mut buf).unwrap();

// }

cc.dump(w)
Ok(())
}

#[inline]
Expand Down Expand Up @@ -69,50 +124,6 @@ fn lua_dump_block() -> io::Result<()> {

impl CompiledCode for LuaCode {
fn dump(&self, w: &mut dyn Write) -> io::Result<()> {
// Lua header
lua_dump_bytes(w, LUA_SIGNATURE.as_bytes())?;
// Lua version, 5.4
lua_dump_byte(w, 5 * 16 + 4)?;
// format, now is zero
lua_dump_byte(w, 0)?;
// data
lua_dump_bytes(w, LUAC_DATA)?;
// size of Lua instruction
lua_dump_byte(w, 4)?;
// size of Lua integer
lua_dump_byte(w, 8)?;
// size of Lua Number
lua_dump_byte(w, 8)?;
// LUAC_INT
lua_dump_bytes(w, &LUAC_INT.to_le_bytes())?;
// LUAC_NUMBER
lua_dump_bytes(w, &LUAC_NUMBER.to_le_bytes())?;

// TODO: size of UpValues in byte, write 0 in temp
lua_dump_byte(w, 0)?;
// TODO: source file name
lua_dump_int(w, 0)?;
// TODO: linedefined
lua_dump_int(w, 0)?;
// TODO: lastlinedefined
lua_dump_int(w, 0)?;
// TODO: numparams
lua_dump_byte(w, 0)?;
// TODO: is_vararg
lua_dump_byte(w, 0)?;
// TODO: maxstacksize
lua_dump_byte(w, 0)?;

// Dump Code

// Dump Constants

// Dump UpValues

// Dump Protos

// Dump Debug

Ok(())
}
}
Expand Down
12 changes: 8 additions & 4 deletions lib/src/backend/lua/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Default for LuaBackendAttribute {
}
}

pub struct LuaBackend {
pub struct LuaBackendCtx {
mgr: UnitsManager,
app: ModuleContext,
byte_codes: Vec<LuaByteCode>,
Expand All @@ -65,7 +65,7 @@ pub struct LuaBackend {
reg_mgr: RegisterManager,
}

impl LuaBackend {
impl LuaBackendCtx {
fn push_attribute_with_scope(&mut self, scope: Scope) {
let attr = LuaBackendAttribute {
scope: Some(scope),
Expand Down Expand Up @@ -126,7 +126,7 @@ impl LuaBackend {
}
}

impl CodeGenBackend for LuaBackend {
impl CodeGenBackend for LuaBackendCtx {
type Label = usize;

fn new(mgr: UnitsManager, app: ModuleContext) -> Self {
Expand Down Expand Up @@ -178,9 +178,13 @@ impl CodeGenBackend for LuaBackend {

self.visit_expression_mut(&mut operands[0]);
}

fn get_bytes<W: Write>(&mut self, w: &mut W) -> io::Result<()> {
lua_dump_module(self, w)
}
}

impl AstVisitorMut for LuaBackend {
impl AstVisitorMut for LuaBackendCtx {
fn visit_literal_mut(&mut self, literal: &mut LiteralExpression) {
trace!("LuaGen: literal expression: {:?}", literal);

Expand Down
19 changes: 9 additions & 10 deletions lib/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
mod llvm;
mod lua;

pub use lua::dump::lua_dump_function;
pub use lua::LuaBackend;
pub use lua::dump::lua_dump_module;
pub use lua::LuaBackendCtx;

use crate::ast::{OperatorExpression, Variable};
use crate::context::{ModuleContext, UnitsManager};
Expand All @@ -14,7 +14,6 @@ use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::io;
use std::io::Write;
use std::marker::PhantomData;

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
Expand All @@ -35,6 +34,7 @@ pub trait CodeGenBackend {
fn define_label<S: AsRef<str>>(&mut self, label: Option<S>) -> Self::Label;
fn gen_variable_load(&mut self, variable: &mut Variable);
fn gen_operator(&mut self, operator: &mut OperatorExpression);
fn get_bytes<W: Write>(&mut self, w: &mut W) -> io::Result<()>;
}

pub trait CompiledCode: Display {
Expand Down Expand Up @@ -71,7 +71,7 @@ where
{
mgr: UnitsManager,
app: ModuleContext,
_backend: PhantomData<B>,
backend: B,
}

impl<B> CodeGenerator<B>
Expand All @@ -87,15 +87,14 @@ where
Ok(Self {
mgr: mgr.clone(),
app: app.clone(),
_backend: PhantomData,
backend: B::new(mgr, app),
})
}
}

impl<B> CodeGenerator<B>
where
B: CodeGenBackend,
{
pub fn get_bytes<W: Write>(&mut self, w: &mut W) -> io::Result<()> {
self.backend.get_bytes(w)
}

pub fn build_application(&mut self) -> Result<(), CodeGenError> {
let mut decl_info: Vec<(_, _)> = self
.app
Expand Down
24 changes: 9 additions & 15 deletions viewer/src/stc_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use gtk4::prelude::*;
use gtk4::{Button, SearchEntry, TextBuffer, TextView, TreeStore, TreeView, TreeViewColumn};
use log::info;
use stc::analysis::TypeAnalyzer;
use stc::backend::{lua_dump_function, CodeGenerator, LuaBackend};
use stc::backend::{CodeGenerator, LuaBackendCtx};
use stc::prelude::*;
use stc::utils::write_ast_to_file;

Expand Down Expand Up @@ -202,27 +202,21 @@ impl StcViewerApp {
}
}

let mut code_gen: CodeGenerator<LuaBackend> =
let mut code_gen: CodeGenerator<LuaBackendCtx> =
CodeGenerator::new(self.mgr.clone(), app_id).unwrap();
println!("CodeGen: {:?}", code_gen.build_application());

if let Some(main) = app_read.find_declaration_by_name(&StString::new("main")) {
let id = main.read().unwrap().id();
let func = app_read.get_function(id).unwrap();
let mut buf = vec![0u8; 0];
code_gen.get_bytes(&mut buf).unwrap();

let mut buf = vec![0u8; 0];
lua_dump_function(func, &mut buf).unwrap();
for (i, v) in buf.iter().enumerate() {
print!("{:0>2x} ", v);

for (i, v) in buf.iter().enumerate() {
print!("{:0>2x} ", v);

if i % 16 == 15 {
println!();
}
if i % 16 == 15 {
println!();
}

println!()
}
println!()
}

pub fn run(&mut self) {}
Expand Down
2 changes: 1 addition & 1 deletion viewer/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct POUList {
}

#[derive(Serialize, Deserialize, Debug)]
#[allow(clippy::upper_case_acronyms)]
#[allow(clippy::upper_case_acronyms)] // Allow upper case POU naming
pub struct POU {
#[serde(rename = "@uuid-text")]
pub uuid_text: Option<String>,
Expand Down

0 comments on commit 7ee5de3

Please sign in to comment.