Skip to content

Commit

Permalink
feat: Implements lua constants dump
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Apr 13, 2024
1 parent b17bd0a commit 7077509
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 23 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ smallvec = { version = "*", features = ["union", "const_generics", "const_new"]
smallmap = "*"
log = "*"
indexmap = "*"
byteorder = "*"
uuid = { version = "*", features = ["v4", "fast-rng", "macro-diagnostics", "serde"] }
inkwell = { version = "*", optional = true, features = ["llvm16-0-no-llvm-linking"] }

Expand Down
29 changes: 20 additions & 9 deletions lib/src/backend/lua/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::ast::SmallVec8;
use crate::parser::StString;

use super::register::Register;
use super::ConstantIndex;
use super::{ConstantIndex, LuaType};

macro_rules! excess_k {
($v: expr, $k: expr) => {
Expand Down Expand Up @@ -149,6 +149,17 @@ pub enum LuaConstants {
Function(fn(&mut LuaExecState) -> i32),
}

impl LuaConstants {
pub fn lua_type(&self) -> LuaType {
match *self {
Self::Nil => LuaType::NIL,
Self::Function(..) => LuaType::FUNCTION,
Self::String(..) => LuaType::STRING,
Self::Float(..) | Self::Integer(..) => LuaType::NUMBER,
}
}
}

#[derive(Debug, Clone, Default)]
pub struct LuaUpValue {
pub name: Option<StString>,
Expand Down Expand Up @@ -274,7 +285,7 @@ impl LuaByteCode {
// AsBx
LuaByteCode::LoadI(a, sbx) => excess_sBx!(sbx) << 8 | a.num() as u32,
// A B
LuaByteCode::Move(a, b) => todo!(),
LuaByteCode::Move(a, b) => (b.num() as u32) << 9 | a.num() as u32,
// A only
LuaByteCode::VarArgPrep(a) => (a as u32) << 8,
};
Expand Down Expand Up @@ -311,17 +322,17 @@ impl LuaCompiledCode {
match code {
// ABC
LuaByteCode::Add(a, b, c) => {
write!(s, "{} {} {}", a.num(), b.num(), c.num()).unwrap();
write!(s, "R{} R{} R{}", a.num(), b.num(), c.num()).unwrap();
}
// A B k
LuaByteCode::Eq(a, b, k) => {
write!(s, "{} {} {k}", a.num(), b.num()).unwrap();
write!(s, "R{} R{} {k}", a.num(), b.num()).unwrap();
}
// A sB k
LuaByteCode::GetTabUp(a, b, c)
| LuaByteCode::Gti(a, b, c)
| LuaByteCode::Gei(a, b, c) => {
write!(s, "{} {b} {c}", a.num()).unwrap();
write!(s, "R{} {b} {c}", a.num()).unwrap();
}
// A B C all literal
LuaByteCode::Call(a, b, c) => {
Expand All @@ -333,15 +344,15 @@ impl LuaCompiledCode {
}
// ABx
LuaByteCode::LoadK(a, bx) => {
write!(s, "{} {bx}", a.num()).unwrap();
write!(s, "R{} {bx}", a.num()).unwrap();
}
// AsBx
LuaByteCode::LoadI(a, sbx) => {
write!(s, "{} {sbx}", a.num()).unwrap();
write!(s, "R{} {sbx}", a.num()).unwrap();
}
// A B
LuaByteCode::Move(a, b) => {
write!(s, "{} {}", a.num(), b.num()).unwrap();
write!(s, "R{} R{}", a.num(), b.num()).unwrap();
}
// A only
LuaByteCode::VarArgPrep(a) => {
Expand All @@ -351,7 +362,7 @@ impl LuaCompiledCode {

match code {
LuaByteCode::LoadK(a, bx) => {
write!(s, " ; {}", self.constants[*bx as usize]).unwrap();
write!(s, " ; K[{}] = {}", bx, self.constants[*bx as usize]).unwrap();
}
LuaByteCode::GetTabUp(a, b, c) => {
write!(s, " ; _ENV \"{}\"", self.constants[*c as usize]).unwrap();
Expand Down
37 changes: 24 additions & 13 deletions lib/src/backend/lua/dump.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::utils::*;
use byteorder::{LittleEndian, WriteBytesExt};

use super::{utils::*, LuaConstants};
use super::{Function, LuaBackend, Prototype};

use crate::backend::lua::bytecode::LuaCompiledCode;
Expand Down Expand Up @@ -65,9 +67,9 @@ fn lua_dump_function(
// TODO: source file name
lua_dump_string(w, None)?;
// TODO: linedefined
lua_dump_int(w, 0)?;
lua_dump_size(w, 0)?;
// TODO: lastlinedefined
lua_dump_int(w, 0)?;
lua_dump_size(w, 0)?;
// numparams
lua_dump_byte(w, num_params(p))?;
// is_vararg
Expand All @@ -84,19 +86,23 @@ fn lua_dump_function(

// Dump Code
for c in lua_code.byte_codes() {
let data = c.encode();
w.write_all(&[
(data & 0xff) as u8,
((data >> 8) & 0xff) as u8,
((data >> 16) & 0xff) as u8,
((data >> 24) & 0xff) as u8,
])?;
w.write_u32::<LittleEndian>(c.encode())?;
}

// Dump size of constants
lua_dump_size(w, lua_code.constants_len() as u64)?;

// Dump Constants
for constant in lua_code.constants() {
lua_dump_byte(w, constant.lua_type().bits())?;

match *constant {
LuaConstants::Integer(i) => lua_dump_integer(w, i)?,
LuaConstants::Float(f) => lua_dump_float(w, f)?,
LuaConstants::String(ref s) => lua_dump_string(w, Some(s))?,
_ => todo!(),
}
}

// Dump size of UpValues
lua_dump_size(w, lua_code.upvalues.len() as u64)?;
Expand Down Expand Up @@ -128,8 +134,13 @@ fn lua_dump_function(
}

#[inline]
fn lua_dump_int(w: &mut dyn Write, i: i32) -> io::Result<()> {
lua_dump_size(w, i as u32 as u64)
fn lua_dump_integer(w: &mut dyn Write, n: i64) -> io::Result<()> {
w.write_i64::<LittleEndian>(n)
}

#[inline]
fn lua_dump_float(w: &mut dyn Write, f: f64) -> io::Result<()> {
w.write_f64::<LittleEndian>(f)
}

#[inline]
Expand Down Expand Up @@ -170,7 +181,7 @@ fn lua_dump_bytes(w: &mut dyn Write, bytes: &[u8]) -> io::Result<()> {
}

#[inline]
fn lua_dump_string(w: &mut dyn Write, opt_str: Option<String>) -> io::Result<()> {
fn lua_dump_string(w: &mut dyn Write, opt_str: Option<&String>) -> io::Result<()> {
match opt_str {
Some(s) => todo!(),
None => lua_dump_size(w, 0),
Expand Down
19 changes: 18 additions & 1 deletion lib/src/backend/lua/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ bitflags! {
}
}

bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LuaType: u8 {
const NONE = 0xff;

const NIL = 0;
const BOOLEAN = 1;
const LIGHTUSERDATA = 2;
const NUMBER = 3;
const STRING = 4;
const TABLE = 5;
const FUNCTION = 6;
const USERDATA = 7;
const THREAD = 8;
}
}

#[derive(Clone)]
pub struct LuaBackendStates {
variable: Option<Rc<Variable>>,
Expand Down Expand Up @@ -358,7 +375,7 @@ impl AstVisitorMut for LuaBackend {
}

self.push_code(LuaByteCode::Call(
callee_index.unwrap() as u8,
callee_index.unwrap(),
call.arguments().len() as u8,
0,
))
Expand Down
1 change: 1 addition & 0 deletions viewer/test_projects/example1/test_proj.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ end_program
</interface>
<body>
a := 123;
a := b;
</body>
</pou>
</pou-list>
Expand Down

0 comments on commit 7077509

Please sign in to comment.