From 9dc5290f78a0857fb75f04240801d90add9f587b Mon Sep 17 00:00:00 2001 From: Nilton Constantino Date: Tue, 27 Jan 2026 13:18:50 +0000 Subject: [PATCH] pr 02 --- crates/prometeu-compiler/src/ir_core/block.rs | 12 ++++ .../prometeu-compiler/src/ir_core/function.rs | 11 ++++ crates/prometeu-compiler/src/ir_core/ids.rs | 16 +++++ crates/prometeu-compiler/src/ir_core/instr.rs | 11 ++++ crates/prometeu-compiler/src/ir_core/mod.rs | 15 +++++ .../prometeu-compiler/src/ir_core/module.rs | 9 +++ .../prometeu-compiler/src/ir_core/program.rs | 8 +++ .../src/ir_core/terminator.rs | 10 +++ crates/prometeu-compiler/src/lib.rs | 1 + .../prometeu-compiler/tests/ir_core_tests.rs | 62 +++++++++++++++++++ docs/specs/pbs/PRs para Junie.md | 3 +- 11 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 crates/prometeu-compiler/src/ir_core/block.rs create mode 100644 crates/prometeu-compiler/src/ir_core/function.rs create mode 100644 crates/prometeu-compiler/src/ir_core/ids.rs create mode 100644 crates/prometeu-compiler/src/ir_core/instr.rs create mode 100644 crates/prometeu-compiler/src/ir_core/mod.rs create mode 100644 crates/prometeu-compiler/src/ir_core/module.rs create mode 100644 crates/prometeu-compiler/src/ir_core/program.rs create mode 100644 crates/prometeu-compiler/src/ir_core/terminator.rs create mode 100644 crates/prometeu-compiler/tests/ir_core_tests.rs diff --git a/crates/prometeu-compiler/src/ir_core/block.rs b/crates/prometeu-compiler/src/ir_core/block.rs new file mode 100644 index 00000000..071d6752 --- /dev/null +++ b/crates/prometeu-compiler/src/ir_core/block.rs @@ -0,0 +1,12 @@ +use serde::{Deserialize, Serialize}; +use super::instr::Instr; +use super::terminator::Terminator; + +/// A basic block in a function's control flow graph. +/// Contains a sequence of instructions and ends with a terminator. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Block { + pub id: u32, + pub instrs: Vec, + pub terminator: Terminator, +} diff --git a/crates/prometeu-compiler/src/ir_core/function.rs b/crates/prometeu-compiler/src/ir_core/function.rs new file mode 100644 index 00000000..96e79640 --- /dev/null +++ b/crates/prometeu-compiler/src/ir_core/function.rs @@ -0,0 +1,11 @@ +use serde::{Deserialize, Serialize}; +use super::ids::FunctionId; +use super::block::Block; + +/// A function within a module, composed of basic blocks forming a CFG. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Function { + pub id: FunctionId, + pub name: String, + pub blocks: Vec, +} diff --git a/crates/prometeu-compiler/src/ir_core/ids.rs b/crates/prometeu-compiler/src/ir_core/ids.rs new file mode 100644 index 00000000..d56390ba --- /dev/null +++ b/crates/prometeu-compiler/src/ir_core/ids.rs @@ -0,0 +1,16 @@ +use serde::{Deserialize, Serialize}; + +/// Unique identifier for a function within a program. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(transparent)] +pub struct FunctionId(pub u32); + +/// Unique identifier for a constant value. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(transparent)] +pub struct ConstId(pub u32); + +/// Unique identifier for a type. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(transparent)] +pub struct TypeId(pub u32); diff --git a/crates/prometeu-compiler/src/ir_core/instr.rs b/crates/prometeu-compiler/src/ir_core/instr.rs new file mode 100644 index 00000000..be0f12a9 --- /dev/null +++ b/crates/prometeu-compiler/src/ir_core/instr.rs @@ -0,0 +1,11 @@ +use serde::{Deserialize, Serialize}; +use super::ids::{ConstId, FunctionId}; + +/// Instructions within a basic block. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum Instr { + /// Placeholder for constant loading. + PushConst(ConstId), + /// Placeholder for function calls. + Call(FunctionId), +} diff --git a/crates/prometeu-compiler/src/ir_core/mod.rs b/crates/prometeu-compiler/src/ir_core/mod.rs new file mode 100644 index 00000000..1994c7c4 --- /dev/null +++ b/crates/prometeu-compiler/src/ir_core/mod.rs @@ -0,0 +1,15 @@ +pub mod ids; +pub mod program; +pub mod module; +pub mod function; +pub mod block; +pub mod instr; +pub mod terminator; + +pub use ids::*; +pub use program::*; +pub use module::*; +pub use function::*; +pub use block::*; +pub use instr::*; +pub use terminator::*; diff --git a/crates/prometeu-compiler/src/ir_core/module.rs b/crates/prometeu-compiler/src/ir_core/module.rs new file mode 100644 index 00000000..0852011e --- /dev/null +++ b/crates/prometeu-compiler/src/ir_core/module.rs @@ -0,0 +1,9 @@ +use serde::{Deserialize, Serialize}; +use super::function::Function; + +/// A module within a program, containing functions and other declarations. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Module { + pub name: String, + pub functions: Vec, +} diff --git a/crates/prometeu-compiler/src/ir_core/program.rs b/crates/prometeu-compiler/src/ir_core/program.rs new file mode 100644 index 00000000..d341526c --- /dev/null +++ b/crates/prometeu-compiler/src/ir_core/program.rs @@ -0,0 +1,8 @@ +use serde::{Deserialize, Serialize}; +use super::module::Module; + +/// A complete PBS program, consisting of multiple modules. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Program { + pub modules: Vec, +} diff --git a/crates/prometeu-compiler/src/ir_core/terminator.rs b/crates/prometeu-compiler/src/ir_core/terminator.rs new file mode 100644 index 00000000..965986dc --- /dev/null +++ b/crates/prometeu-compiler/src/ir_core/terminator.rs @@ -0,0 +1,10 @@ +use serde::{Deserialize, Serialize}; + +/// Terminators that end a basic block and handle control flow. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum Terminator { + /// Returns from the current function. + Return, + /// Unconditional jump to another block (by index/ID). + Jump(u32), +} diff --git a/crates/prometeu-compiler/src/lib.rs b/crates/prometeu-compiler/src/lib.rs index d9e9d2ac..45265994 100644 --- a/crates/prometeu-compiler/src/lib.rs +++ b/crates/prometeu-compiler/src/lib.rs @@ -39,6 +39,7 @@ pub mod common; pub mod ir; +pub mod ir_core; pub mod backend; pub mod frontends; pub mod compiler; diff --git a/crates/prometeu-compiler/tests/ir_core_tests.rs b/crates/prometeu-compiler/tests/ir_core_tests.rs new file mode 100644 index 00000000..d8c839d7 --- /dev/null +++ b/crates/prometeu-compiler/tests/ir_core_tests.rs @@ -0,0 +1,62 @@ +use prometeu_compiler::ir_core::*; +use serde_json; + +#[test] +fn test_ir_core_manual_construction() { + let program = Program { + modules: vec![Module { + name: "main".to_string(), + functions: vec![Function { + id: FunctionId(10), + name: "entry".to_string(), + blocks: vec![Block { + id: 0, + instrs: vec![ + Instr::PushConst(ConstId(0)), + Instr::Call(FunctionId(11)), + ], + terminator: Terminator::Return, + }], + }], + }], + }; + + let json = serde_json::to_string_pretty(&program).unwrap(); + + // Snapshot check for deterministic shape + let expected = r#"{ + "modules": [ + { + "name": "main", + "functions": [ + { + "id": 10, + "name": "entry", + "blocks": [ + { + "id": 0, + "instrs": [ + { + "PushConst": 0 + }, + { + "Call": 11 + } + ], + "terminator": "Return" + } + ] + } + ] + } + ] +}"#; + 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"); +} diff --git a/docs/specs/pbs/PRs para Junie.md b/docs/specs/pbs/PRs para Junie.md index 0e15e8fd..a9e40a76 100644 --- a/docs/specs/pbs/PRs para Junie.md +++ b/docs/specs/pbs/PRs para Junie.md @@ -12,7 +12,7 @@ > * Each PR must include tests. > * No speculative features. > * Follow the `Prometeu Base Script (PBS) - Implementation Spec`. -> * Do not touch any other places in the codebase just frontend. If you need to touch other places, request it with comment/output in the PR. +> * VM IR is frozen: new opcodes are forbidden unless explicitly planned in a PR titled “VM Instruction Set Change” with a full rationale + golden bytecode tests. --- @@ -26,7 +26,6 @@ * VM IR remains simple and stable. * Lowering is explicit and testable. - --- # PR-01 — ProjectConfig and Frontend Selection