dev/pbs #8

Merged
bquarkz merged 74 commits from dev/pbs into master 2026-02-03 15:28:31 +00:00
11 changed files with 156 additions and 2 deletions
Showing only changes of commit 9dc5290f78 - Show all commits

View File

@ -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<Instr>,
pub terminator: Terminator,
}

View File

@ -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<Block>,
}

View File

@ -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);

View File

@ -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),
}

View File

@ -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::*;

View File

@ -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<Function>,
}

View File

@ -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<Module>,
}

View File

@ -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),
}

View File

@ -39,6 +39,7 @@
pub mod common;
pub mod ir;
pub mod ir_core;
pub mod backend;
pub mod frontends;
pub mod compiler;

View File

@ -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");
}

View File

@ -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