Enum rslua_march1917::compiler::ExprResult
source · Variants§
Implementations§
source§impl ExprResult
impl ExprResult
sourcepub fn new_const(k: Const) -> Self
pub fn new_const(k: Const) -> Self
Examples found in repository?
src/compiler.rs (line 250)
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
fn expr(&mut self, expr: &Expr, reg: Option<u32>) -> Result<ExprResult, CompileError> {
let proto = self.proto();
let result = match expr {
Expr::Int(i) => ExprResult::new_const(Const::Int(*i)),
Expr::Float(f) => ExprResult::new_const(Const::Float(*f)),
Expr::String(s) => {
// const string will always be added to consts
let k = Const::Str(s.clone());
proto.add_const(k.clone());
ExprResult::new_const(k)
}
Expr::Nil => ExprResult::Nil,
Expr::True => ExprResult::True,
Expr::False => ExprResult::False,
Expr::Name(name) => {
if let Some(src) = proto.get_local_var(name) {
return Ok(ExprResult::new_const_reg(src));
}
// TODO : process upval and globals
todo!()
}
Expr::BinExpr(_) | Expr::UnExpr(_) => self.folding_or_code(expr, reg)?,
Expr::ParenExpr(expr) => self.folding_or_code(&expr, reg)?,
_ => todo!(),
};
Ok(result)
}
// try constant foding first, if failed then generate code
fn folding_or_code(
&mut self,
expr: &Expr,
reg: Option<u32>,
) -> Result<ExprResult, CompileError> {
if let Some(k) = self.try_const_folding(expr)? {
Ok(ExprResult::new_const(k))
} else {
self.code_expr(expr, reg)
}
}sourcepub fn new_const_reg(reg: u32) -> Self
pub fn new_const_reg(reg: u32) -> Self
Examples found in repository?
src/compiler.rs (line 263)
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
fn expr(&mut self, expr: &Expr, reg: Option<u32>) -> Result<ExprResult, CompileError> {
let proto = self.proto();
let result = match expr {
Expr::Int(i) => ExprResult::new_const(Const::Int(*i)),
Expr::Float(f) => ExprResult::new_const(Const::Float(*f)),
Expr::String(s) => {
// const string will always be added to consts
let k = Const::Str(s.clone());
proto.add_const(k.clone());
ExprResult::new_const(k)
}
Expr::Nil => ExprResult::Nil,
Expr::True => ExprResult::True,
Expr::False => ExprResult::False,
Expr::Name(name) => {
if let Some(src) = proto.get_local_var(name) {
return Ok(ExprResult::new_const_reg(src));
}
// TODO : process upval and globals
todo!()
}
Expr::BinExpr(_) | Expr::UnExpr(_) => self.folding_or_code(expr, reg)?,
Expr::ParenExpr(expr) => self.folding_or_code(&expr, reg)?,
_ => todo!(),
};
Ok(result)
}sourcepub fn new_jump(reg: Reg, pc: usize) -> Self
pub fn new_jump(reg: Reg, pc: usize) -> Self
Examples found in repository?
src/compiler.rs (line 466)
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
fn code_comp(&mut self, op: BinOp, target: ExprResult, left: u32, right: u32) -> ExprResult {
match target {
ExprResult::Reg(reg) => {
// covert >= to <=, > to <
let (left, right) = match op {
BinOp::Ge | BinOp::Gt => (right, left),
_ => (left, right),
};
let proto = self.proto();
proto.code_comp(op, left, right);
let jump = proto.code_jmp(NO_JUMP, 0);
ExprResult::new_jump(reg, jump)
}
_ => unreachable!(),
}
}sourcepub fn get_rk(&self, context: &mut ProtoContext) -> u32
pub fn get_rk(&self, context: &mut ProtoContext) -> u32
Examples found in repository?
src/compiler.rs (line 434)
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
fn code_bin_op(
&mut self,
op: BinOp,
input: Option<u32>,
left_expr: &Expr,
right_expr: &Expr,
) -> Result<ExprResult, CompileError> {
// get left expr result
let left = self.expr(left_expr, input)?;
// resolve previous expr result
left.resolve(self.context());
// if input reg is not used by left expr, apply it to right expr
let right_input = self.get_right_input(input, &left);
// get right expr result
let right = self.expr(right_expr, right_input)?;
// resolve previous expr result
right.resolve(self.context());
let alloc_reg = self.alloc_reg(&input);
let reg = alloc_reg.reg;
let mut result = ExprResult::Reg(alloc_reg);
// get rk of left and right expr
let mut get_rk = || {
let left_rk = left.get_rk(self.context());
let right_rk = right.get_rk(self.context());
(left_rk, right_rk)
};
// gennerate opcode of binop
match op {
_ if op.is_comp() => {
let (left_rk, right_rk) = get_rk();
result = self.code_comp(op, result, left_rk, right_rk);
}
_ => {
let (left_rk, right_rk) = get_rk();
self.proto().code_bin_op(op, reg, left_rk, right_rk);
}
};
Ok(result)
}
fn code_comp(&mut self, op: BinOp, target: ExprResult, left: u32, right: u32) -> ExprResult {
match target {
ExprResult::Reg(reg) => {
// covert >= to <=, > to <
let (left, right) = match op {
BinOp::Ge | BinOp::Gt => (right, left),
_ => (left, right),
};
let proto = self.proto();
proto.code_comp(op, left, right);
let jump = proto.code_jmp(NO_JUMP, 0);
ExprResult::new_jump(reg, jump)
}
_ => unreachable!(),
}
}
fn code_and(
&mut self,
input: Option<u32>,
left_expr: &Expr,
right_expr: &Expr,
) -> Result<ExprResult, CompileError> {
// get left expr result
let mut left = self.expr(left_expr, input)?;
match &mut left {
// do const folding if left is const value
ExprResult::True | ExprResult::Const(_) => self.expr(right_expr, input),
ExprResult::Jump(j) => {
j.inverse_cond(self.context());
let mut right = self.expr(right_expr, Some(j.reg.reg))?;
match &mut right {
ExprResult::Jump(rj) => rj.concat_false_jumps(j),
_ => todo!(),
};
Ok(right)
}
ExprResult::Reg(_reg) => self.code_test(input, left, right_expr),
_ => todo!(),
}
}
fn code_test(
&mut self,
input: Option<u32>,
left: ExprResult,
right: &Expr,
) -> Result<ExprResult, CompileError> {
match &left {
ExprResult::Reg(r) => {
let proto = self.proto();
proto.code_test_set(NO_REG, r.reg, 0);
let jump = proto.code_jmp(NO_JUMP, 0);
let right_input = self.get_right_input(input, &left);
let right_result = self.expr(right, right_input)?;
let mut jump = Jump::new(self.alloc_reg(&input), jump);
match &right_result {
ExprResult::Reg(r) if r.is_const() => jump.set_reg_should_move(r.reg),
_ => (),
};
Ok(ExprResult::Jump(jump))
}
_ => unreachable!(),
}
}
fn code_un_op(
&mut self,
op: UnOp,
input: Option<u32>,
expr: ExprResult,
) -> Result<ExprResult, CompileError> {
let src = expr.get_rk(self.context());
// resolve previous result
expr.resolve(self.context());
let alloc_reg = self.alloc_reg(&input);
let reg = alloc_reg.reg;
let result = ExprResult::Reg(alloc_reg);
// gennerate opcode of unop
let proto = self.proto();
proto.code_un_op(op, reg, src);
Ok(result)
}sourcepub fn resolve(&self, context: &mut ProtoContext)
pub fn resolve(&self, context: &mut ProtoContext)
Examples found in repository?
src/compiler.rs (line 417)
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
fn code_bin_op(
&mut self,
op: BinOp,
input: Option<u32>,
left_expr: &Expr,
right_expr: &Expr,
) -> Result<ExprResult, CompileError> {
// get left expr result
let left = self.expr(left_expr, input)?;
// resolve previous expr result
left.resolve(self.context());
// if input reg is not used by left expr, apply it to right expr
let right_input = self.get_right_input(input, &left);
// get right expr result
let right = self.expr(right_expr, right_input)?;
// resolve previous expr result
right.resolve(self.context());
let alloc_reg = self.alloc_reg(&input);
let reg = alloc_reg.reg;
let mut result = ExprResult::Reg(alloc_reg);
// get rk of left and right expr
let mut get_rk = || {
let left_rk = left.get_rk(self.context());
let right_rk = right.get_rk(self.context());
(left_rk, right_rk)
};
// gennerate opcode of binop
match op {
_ if op.is_comp() => {
let (left_rk, right_rk) = get_rk();
result = self.code_comp(op, result, left_rk, right_rk);
}
_ => {
let (left_rk, right_rk) = get_rk();
self.proto().code_bin_op(op, reg, left_rk, right_rk);
}
};
Ok(result)
}
fn code_comp(&mut self, op: BinOp, target: ExprResult, left: u32, right: u32) -> ExprResult {
match target {
ExprResult::Reg(reg) => {
// covert >= to <=, > to <
let (left, right) = match op {
BinOp::Ge | BinOp::Gt => (right, left),
_ => (left, right),
};
let proto = self.proto();
proto.code_comp(op, left, right);
let jump = proto.code_jmp(NO_JUMP, 0);
ExprResult::new_jump(reg, jump)
}
_ => unreachable!(),
}
}
fn code_and(
&mut self,
input: Option<u32>,
left_expr: &Expr,
right_expr: &Expr,
) -> Result<ExprResult, CompileError> {
// get left expr result
let mut left = self.expr(left_expr, input)?;
match &mut left {
// do const folding if left is const value
ExprResult::True | ExprResult::Const(_) => self.expr(right_expr, input),
ExprResult::Jump(j) => {
j.inverse_cond(self.context());
let mut right = self.expr(right_expr, Some(j.reg.reg))?;
match &mut right {
ExprResult::Jump(rj) => rj.concat_false_jumps(j),
_ => todo!(),
};
Ok(right)
}
ExprResult::Reg(_reg) => self.code_test(input, left, right_expr),
_ => todo!(),
}
}
fn code_test(
&mut self,
input: Option<u32>,
left: ExprResult,
right: &Expr,
) -> Result<ExprResult, CompileError> {
match &left {
ExprResult::Reg(r) => {
let proto = self.proto();
proto.code_test_set(NO_REG, r.reg, 0);
let jump = proto.code_jmp(NO_JUMP, 0);
let right_input = self.get_right_input(input, &left);
let right_result = self.expr(right, right_input)?;
let mut jump = Jump::new(self.alloc_reg(&input), jump);
match &right_result {
ExprResult::Reg(r) if r.is_const() => jump.set_reg_should_move(r.reg),
_ => (),
};
Ok(ExprResult::Jump(jump))
}
_ => unreachable!(),
}
}
fn code_un_op(
&mut self,
op: UnOp,
input: Option<u32>,
expr: ExprResult,
) -> Result<ExprResult, CompileError> {
let src = expr.get_rk(self.context());
// resolve previous result
expr.resolve(self.context());
let alloc_reg = self.alloc_reg(&input);
let reg = alloc_reg.reg;
let result = ExprResult::Reg(alloc_reg);
// gennerate opcode of unop
let proto = self.proto();
proto.code_un_op(op, reg, src);
Ok(result)
}