pr 02
This commit is contained in:
parent
38beebba9b
commit
9dc5290f78
12
crates/prometeu-compiler/src/ir_core/block.rs
Normal file
12
crates/prometeu-compiler/src/ir_core/block.rs
Normal 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,
|
||||
}
|
||||
11
crates/prometeu-compiler/src/ir_core/function.rs
Normal file
11
crates/prometeu-compiler/src/ir_core/function.rs
Normal 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>,
|
||||
}
|
||||
16
crates/prometeu-compiler/src/ir_core/ids.rs
Normal file
16
crates/prometeu-compiler/src/ir_core/ids.rs
Normal 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);
|
||||
11
crates/prometeu-compiler/src/ir_core/instr.rs
Normal file
11
crates/prometeu-compiler/src/ir_core/instr.rs
Normal 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),
|
||||
}
|
||||
15
crates/prometeu-compiler/src/ir_core/mod.rs
Normal file
15
crates/prometeu-compiler/src/ir_core/mod.rs
Normal 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::*;
|
||||
9
crates/prometeu-compiler/src/ir_core/module.rs
Normal file
9
crates/prometeu-compiler/src/ir_core/module.rs
Normal 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>,
|
||||
}
|
||||
8
crates/prometeu-compiler/src/ir_core/program.rs
Normal file
8
crates/prometeu-compiler/src/ir_core/program.rs
Normal 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>,
|
||||
}
|
||||
10
crates/prometeu-compiler/src/ir_core/terminator.rs
Normal file
10
crates/prometeu-compiler/src/ir_core/terminator.rs
Normal 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),
|
||||
}
|
||||
@ -39,6 +39,7 @@
|
||||
|
||||
pub mod common;
|
||||
pub mod ir;
|
||||
pub mod ir_core;
|
||||
pub mod backend;
|
||||
pub mod frontends;
|
||||
pub mod compiler;
|
||||
|
||||
62
crates/prometeu-compiler/tests/ir_core_tests.rs
Normal file
62
crates/prometeu-compiler/tests/ir_core_tests.rs
Normal 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");
|
||||
}
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user