pr 08 lsp
This commit is contained in:
@@ -4,9 +4,8 @@ use crate::frontends::pbs::ast::*;
|
||||
use crate::frontends::pbs::contracts::ContractRegistry;
|
||||
use crate::frontends::pbs::symbols::*;
|
||||
use crate::frontends::pbs::types::PbsType;
|
||||
use crate::ir_core;
|
||||
use crate::ir_core::ids::{FieldId, FunctionId, TypeId, ValueId};
|
||||
use crate::ir_core::{Block, Function, Instr, InstrKind, Module, Param, Program, Terminator, Type};
|
||||
use crate::ir_core::{Block, ConstPool, Function, Instr, InstrKind, Module, Param, Program, Terminator, Type};
|
||||
use prometeu_analysis::{NameInterner, NodeId};
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -62,7 +61,7 @@ impl<'a> Lowerer<'a> {
|
||||
imported_symbols,
|
||||
interner,
|
||||
program: Program {
|
||||
const_pool: ir_core::ConstPool::new(),
|
||||
const_pool: ConstPool::new(),
|
||||
modules: Vec::new(),
|
||||
field_offsets,
|
||||
field_types: HashMap::new(),
|
||||
@@ -1341,14 +1340,13 @@ impl<'a> Lowerer<'a> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::common::spans::FileId;
|
||||
use crate::frontends::pbs::collector::SymbolCollector;
|
||||
use crate::frontends::pbs::parser::Parser;
|
||||
use crate::frontends::pbs::symbols::ModuleSymbols;
|
||||
use crate::ir_core;
|
||||
use prometeu_analysis::NameInterner;
|
||||
use crate::common::spans::FileId;
|
||||
|
||||
fn lower_program(code: &str) -> ir_core::Program {
|
||||
fn lower_program(code: &str) -> Program {
|
||||
let mut interner = NameInterner::new();
|
||||
let mut parser = Parser::new(code, FileId(0), &mut interner);
|
||||
let parsed = parser.parse_file().expect("Failed to parse");
|
||||
@@ -1383,13 +1381,13 @@ mod tests {
|
||||
|
||||
let add_func = module.functions.iter().find(|f| f.name == "add").unwrap();
|
||||
assert_eq!(add_func.params.len(), 2);
|
||||
assert_eq!(add_func.return_type, ir_core::Type::Int);
|
||||
assert_eq!(add_func.return_type, Type::Int);
|
||||
|
||||
// Verify blocks
|
||||
assert!(add_func.blocks.len() >= 1);
|
||||
let first_block = &add_func.blocks[0];
|
||||
// Check for Add instruction
|
||||
assert!(first_block.instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Add)));
|
||||
assert!(first_block.instrs.iter().any(|i| matches!(i.kind, InstrKind::Add)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1417,15 +1415,15 @@ mod tests {
|
||||
.flat_map(|b| b.instrs.iter())
|
||||
.collect();
|
||||
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Add)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Sub)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Mul)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Div)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Eq)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Lt)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Gt)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::And)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Or)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Add)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Sub)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Mul)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Div)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Eq)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Lt)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Gt)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::And)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Or)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1446,8 +1444,8 @@ mod tests {
|
||||
.flat_map(|b| b.instrs.iter())
|
||||
.collect();
|
||||
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Neg)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Not)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Neg)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Not)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1497,8 +1495,8 @@ mod tests {
|
||||
let main_func = module.functions.iter().find(|f| f.name == "main").unwrap();
|
||||
let terminators: Vec<_> = main_func.blocks.iter().map(|b| &b.terminator).collect();
|
||||
|
||||
assert!(terminators.iter().any(|t| matches!(t, ir_core::Terminator::JumpIfFalse { .. })));
|
||||
assert!(terminators.iter().any(|t| matches!(t, ir_core::Terminator::Jump(_))));
|
||||
assert!(terminators.iter().any(|t| matches!(t, Terminator::JumpIfFalse { .. })));
|
||||
assert!(terminators.iter().any(|t| matches!(t, Terminator::Jump(_))));
|
||||
assert!(main_func.blocks.len() >= 3);
|
||||
}
|
||||
|
||||
@@ -1518,7 +1516,7 @@ mod tests {
|
||||
let main_func = module.functions.iter().find(|f| f.name == "main").unwrap();
|
||||
let terminators: Vec<_> = main_func.blocks.iter().map(|b| &b.terminator).collect();
|
||||
|
||||
assert!(terminators.iter().any(|t| matches!(t, ir_core::Terminator::JumpIfFalse { .. })));
|
||||
assert!(terminators.iter().any(|t| matches!(t, Terminator::JumpIfFalse { .. })));
|
||||
assert!(main_func.blocks.len() >= 5);
|
||||
}
|
||||
|
||||
@@ -1548,24 +1546,24 @@ mod tests {
|
||||
let main_func = module.functions.iter().find(|f| f.name == "main").unwrap();
|
||||
let params: Vec<_> = main_func.params.iter().map(|p| p.ty.clone()).collect();
|
||||
|
||||
assert_eq!(params[0], ir_core::Type::Service("MyService".to_string()));
|
||||
assert_eq!(params[1], ir_core::Type::Contract("MyContract".to_string()));
|
||||
assert_eq!(params[2], ir_core::Type::ErrorType("MyError".to_string()));
|
||||
assert_eq!(params[3], ir_core::Type::Struct("Point".to_string()));
|
||||
assert_eq!(params[0], Type::Service("MyService".to_string()));
|
||||
assert_eq!(params[1], Type::Contract("MyContract".to_string()));
|
||||
assert_eq!(params[2], Type::ErrorType("MyError".to_string()));
|
||||
assert_eq!(params[3], Type::Struct("Point".to_string()));
|
||||
assert_eq!(
|
||||
params[4],
|
||||
ir_core::Type::Optional(Box::new(ir_core::Type::Int))
|
||||
Type::Optional(Box::new(Type::Int))
|
||||
);
|
||||
assert_eq!(
|
||||
params[5],
|
||||
ir_core::Type::Result(
|
||||
Box::new(ir_core::Type::Int),
|
||||
Box::new(ir_core::Type::String)
|
||||
Type::Result(
|
||||
Box::new(Type::Int),
|
||||
Box::new(Type::String)
|
||||
)
|
||||
);
|
||||
assert_eq!(
|
||||
params[6],
|
||||
ir_core::Type::Array(Box::new(ir_core::Type::Int), 3)
|
||||
Type::Array(Box::new(Type::Int), 3)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1591,7 +1589,7 @@ mod tests {
|
||||
|
||||
assert!(instrs
|
||||
.iter()
|
||||
.any(|i| matches!(i.kind, ir_core::InstrKind::Call(_, 2))));
|
||||
.any(|i| matches!(i.kind, InstrKind::Call(_, 2))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1614,10 +1612,10 @@ mod tests {
|
||||
|
||||
assert!(instrs
|
||||
.iter()
|
||||
.any(|i| matches!(i.kind, ir_core::InstrKind::HostCall(_, _))));
|
||||
.any(|i| matches!(i.kind, InstrKind::HostCall(_, _))));
|
||||
assert!(instrs
|
||||
.iter()
|
||||
.any(|i| matches!(i.kind, ir_core::InstrKind::PushBounded(_))));
|
||||
.any(|i| matches!(i.kind, InstrKind::PushBounded(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1643,13 +1641,13 @@ mod tests {
|
||||
|
||||
assert!(instrs
|
||||
.iter()
|
||||
.any(|i| matches!(i.kind, ir_core::InstrKind::GetLocal(16))));
|
||||
.any(|i| matches!(i.kind, InstrKind::GetLocal(16))));
|
||||
assert!(instrs
|
||||
.iter()
|
||||
.any(|i| matches!(i.kind, ir_core::InstrKind::GetLocal(18))));
|
||||
.any(|i| matches!(i.kind, InstrKind::GetLocal(18))));
|
||||
assert!(instrs
|
||||
.iter()
|
||||
.any(|i| matches!(i.kind, ir_core::InstrKind::PushBounded(_))));
|
||||
.any(|i| matches!(i.kind, InstrKind::PushBounded(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1677,12 +1675,12 @@ mod tests {
|
||||
|
||||
let push_consts = instrs
|
||||
.iter()
|
||||
.filter(|i| matches!(i.kind, ir_core::InstrKind::PushConst(_)))
|
||||
.filter(|i| matches!(i.kind, InstrKind::PushConst(_)))
|
||||
.count();
|
||||
|
||||
assert_eq!(push_consts, 3);
|
||||
assert!(!instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Call(_, _))));
|
||||
assert!(!instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::HostCall(_, _))));
|
||||
assert!(!instrs.iter().any(|i| matches!(i.kind, InstrKind::Call(_, _))));
|
||||
assert!(!instrs.iter().any(|i| matches!(i.kind, InstrKind::HostCall(_, _))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1712,9 +1710,9 @@ mod tests {
|
||||
let func = &program.modules[0].functions[0];
|
||||
let instrs: Vec<_> = func.blocks.iter().flat_map(|b| b.instrs.iter()).collect();
|
||||
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::Alloc { .. })));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::BeginMutate { .. })));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::EndMutate)));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::Alloc { .. })));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::BeginMutate { .. })));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::EndMutate)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1828,9 +1826,9 @@ mod tests {
|
||||
let instrs: Vec<_> = func.blocks.iter().flat_map(|b| b.instrs.iter()).collect();
|
||||
|
||||
// Gfx.clear -> 0x1010
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::HostCall(0x1010, 0))));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::HostCall(0x1010, 0))));
|
||||
// Log.write -> 0x5001
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, ir_core::InstrKind::HostCall(0x5001, 0))));
|
||||
assert!(instrs.iter().any(|i| matches!(i.kind, InstrKind::HostCall(0x5001, 0))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user