This commit is contained in:
Nilton Constantino
2026-01-27 13:29:38 +00:00
parent 9dc5290f78
commit 845fc36bd2
9 changed files with 169 additions and 19 deletions
@@ -0,0 +1,42 @@
use prometeu_compiler::ir::module::{Module, Function};
use prometeu_compiler::ir::instr::{Instruction, InstrKind};
use prometeu_compiler::ir::types::Type;
use prometeu_compiler::ir_core::const_pool::ConstantValue;
use prometeu_compiler::backend::emit_module;
use prometeu_compiler::common::files::FileManager;
use prometeu_bytecode::pbc::parse_pbc;
use prometeu_bytecode::pbc::ConstantPoolEntry;
#[test]
fn test_emit_module_with_const_pool() {
let mut module = Module::new("test".to_string());
// Insert constants into IR module
let id_int = module.const_pool.insert(ConstantValue::Int(12345));
let id_str = module.const_pool.insert(ConstantValue::String("hello".to_string()));
let function = Function {
name: "main".to_string(),
params: vec![],
return_type: Type::Void,
body: vec![
Instruction::new(InstrKind::PushConst(id_int), None),
Instruction::new(InstrKind::PushConst(id_str), None),
Instruction::new(InstrKind::Ret, None),
],
};
module.functions.push(function);
let file_manager = FileManager::new();
let result = emit_module(&module, &file_manager).expect("Failed to emit module");
let pbc = parse_pbc(&result.rom).expect("Failed to parse emitted PBC");
// Check constant pool in PBC
// PBC CP has Null at index 0, so our constants should be at 1 and 2
assert_eq!(pbc.cp.len(), 3);
assert_eq!(pbc.cp[0], ConstantPoolEntry::Null);
assert_eq!(pbc.cp[1], ConstantPoolEntry::Int64(12345));
assert_eq!(pbc.cp[2], ConstantPoolEntry::String("hello".to_string()));
}
@@ -0,0 +1,43 @@
use prometeu_compiler::ir_core::const_pool::{ConstPool, ConstantValue};
use prometeu_compiler::ir_core::ids::ConstId;
#[test]
fn test_const_pool_deduplication() {
let mut pool = ConstPool::new();
let id1 = pool.insert(ConstantValue::Int(42));
let id2 = pool.insert(ConstantValue::String("hello".to_string()));
let id3 = pool.insert(ConstantValue::Int(42));
assert_eq!(id1, id3);
assert_ne!(id1, id2);
assert_eq!(pool.constants.len(), 2);
}
#[test]
fn test_const_pool_deterministic_assignment() {
let mut pool = ConstPool::new();
let id0 = pool.insert(ConstantValue::Int(10));
let id1 = pool.insert(ConstantValue::Int(20));
let id2 = pool.insert(ConstantValue::Int(30));
assert_eq!(id0, ConstId(0));
assert_eq!(id1, ConstId(1));
assert_eq!(id2, ConstId(2));
}
#[test]
fn test_const_pool_serialization() {
let mut pool = ConstPool::new();
pool.insert(ConstantValue::Int(42));
pool.insert(ConstantValue::String("test".to_string()));
pool.insert(ConstantValue::Float(3.14));
let json = serde_json::to_string_pretty(&pool).unwrap();
// Check for deterministic shape in JSON
assert!(json.contains("\"Int\": 42"));
assert!(json.contains("\"String\": \"test\""));
assert!(json.contains("\"Float\": 3.14"));
}
@@ -3,7 +3,11 @@ 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 {
@@ -25,6 +29,13 @@ fn test_ir_core_manual_construction() {
// Snapshot check for deterministic shape
let expected = r#"{
"const_pool": {
"constants": [
{
"String": "hello"
}
]
},
"modules": [
{
"name": "main",