Co-authored-by: Nilton Constantino <nilton.constantino@visma.com> Reviewed-on: #8
123 lines
3.0 KiB
Rust
123 lines
3.0 KiB
Rust
pub mod ids;
|
|
pub mod const_pool;
|
|
pub mod types;
|
|
pub mod program;
|
|
pub mod module;
|
|
pub mod function;
|
|
pub mod block;
|
|
pub mod instr;
|
|
pub mod terminator;
|
|
pub mod validate;
|
|
|
|
pub use block::*;
|
|
pub use const_pool::*;
|
|
pub use function::*;
|
|
pub use ids::*;
|
|
pub use instr::*;
|
|
pub use module::*;
|
|
pub use program::*;
|
|
pub use terminator::*;
|
|
pub use types::*;
|
|
pub use validate::*;
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use serde_json;
|
|
|
|
#[test]
|
|
fn test_ir_core_manual_construction() {
|
|
let mut const_pool = ConstPool::new();
|
|
const_pool.insert(ConstantValue::String("hello".to_string()));
|
|
|
|
let program = Program {
|
|
const_pool,
|
|
modules: vec![Module {
|
|
name: "main".to_string(),
|
|
functions: vec![Function {
|
|
id: FunctionId(10),
|
|
name: "entry".to_string(),
|
|
param_slots: 0,
|
|
local_slots: 0,
|
|
return_slots: 0,
|
|
params: vec![],
|
|
return_type: Type::Void,
|
|
blocks: vec![Block {
|
|
id: 0,
|
|
instrs: vec![
|
|
Instr::from(InstrKind::PushConst(ConstId(0))),
|
|
Instr::from(InstrKind::Call(FunctionId(11), 0)),
|
|
],
|
|
terminator: Terminator::Return,
|
|
}],
|
|
local_types: std::collections::HashMap::new(),
|
|
}],
|
|
}],
|
|
field_offsets: std::collections::HashMap::new(),
|
|
field_types: std::collections::HashMap::new(),
|
|
};
|
|
|
|
let json = serde_json::to_string_pretty(&program).unwrap();
|
|
|
|
let expected = r#"{
|
|
"const_pool": {
|
|
"constants": [
|
|
{
|
|
"String": "hello"
|
|
}
|
|
]
|
|
},
|
|
"modules": [
|
|
{
|
|
"name": "main",
|
|
"functions": [
|
|
{
|
|
"id": 10,
|
|
"name": "entry",
|
|
"params": [],
|
|
"return_type": "Void",
|
|
"blocks": [
|
|
{
|
|
"id": 0,
|
|
"instrs": [
|
|
{
|
|
"kind": {
|
|
"PushConst": 0
|
|
},
|
|
"span": null
|
|
},
|
|
{
|
|
"kind": {
|
|
"Call": [
|
|
11,
|
|
0
|
|
]
|
|
},
|
|
"span": null
|
|
}
|
|
],
|
|
"terminator": "Return"
|
|
}
|
|
],
|
|
"local_types": {},
|
|
"param_slots": 0,
|
|
"local_slots": 0,
|
|
"return_slots": 0
|
|
}
|
|
]
|
|
}
|
|
],
|
|
"field_offsets": {},
|
|
"field_types": {}
|
|
}"#;
|
|
assert_eq!(json, expected);
|
|
}
|
|
|
|
#[test]
|
|
fn test_ir_core_ids() {
|
|
assert_eq!(serde_json::to_string(&FunctionId(1)).unwrap(), "1");
|
|
assert_eq!(serde_json::to_string(&ConstId(2)).unwrap(), "2");
|
|
assert_eq!(serde_json::to_string(&TypeId(3)).unwrap(), "3");
|
|
}
|
|
}
|