-
Notifications
You must be signed in to change notification settings - Fork 61
feat(azure-policy): add compiler skeleton with core types and stubs #674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anakrish
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
anakrish:azure-policy-compiler-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| #![allow(clippy::pattern_type_mismatch)] | ||
|
|
||
| //! Constraint / condition / LHS compilation. | ||
| //! | ||
| //! Stub — real implementation added in a later commit. | ||
|
|
||
| use anyhow::{bail, Result}; | ||
|
|
||
| use crate::languages::azure_policy::ast::Constraint; | ||
|
|
||
| use super::core::Compiler; | ||
|
|
||
| impl Compiler { | ||
| pub(super) fn compile_constraint(&mut self, _constraint: &Constraint) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("condition compilation not yet implemented") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Implicit allOf for unbound `[*]` wildcard fields. | ||
| //! | ||
| //! Stub — real implementation added in a later commit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| #![allow(dead_code)] | ||
| #![allow(clippy::pattern_type_mismatch)] | ||
|
|
||
| //! Core `Compiler` struct, main compilation pipeline, and register/emit | ||
| //! infrastructure. | ||
|
|
||
| use alloc::collections::{BTreeMap, BTreeSet}; | ||
| use alloc::string::{String, ToString as _}; | ||
| use alloc::vec::Vec; | ||
|
|
||
| use anyhow::{anyhow, bail, Result}; | ||
|
|
||
| use crate::rvm::program::{Program, SpanInfo}; | ||
| use crate::rvm::Instruction; | ||
| use crate::{Rc, Value}; | ||
|
|
||
| use crate::languages::azure_policy::ast::PolicyRule; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Types | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub(super) struct CountBinding { | ||
| pub(super) name: Option<String>, | ||
| pub(super) field_wildcard_prefix: Option<String>, | ||
| pub(super) current_reg: u8, | ||
| } | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub(super) struct Compiler { | ||
| pub(super) program: Program, | ||
| pub(super) register_counter: u8, | ||
| /// High-water mark of `register_counter`. | ||
| pub(super) register_high_water: u8, | ||
| pub(super) source_to_index: BTreeMap<String, usize>, | ||
| pub(super) builtin_index: BTreeMap<String, u16>, | ||
| pub(super) count_bindings: Vec<CountBinding>, | ||
| /// Cached register for `LoadInput` — allocated once on first use. | ||
| pub(super) cached_input_reg: Option<u8>, | ||
| /// Cached register for `LoadContext` — allocated once on first use. | ||
| pub(super) cached_context_reg: Option<u8>, | ||
| /// Map from lowercase fully-qualified alias name → short name. | ||
| pub(super) alias_map: BTreeMap<String, String>, | ||
| /// Map from lowercase fully-qualified alias name → modifiable flag. | ||
| pub(super) alias_modifiable: BTreeMap<String, bool>, | ||
| /// Default values for policy parameters. | ||
| pub(super) parameter_defaults: Option<Value>, | ||
| /// When set, field conditions resolve against this register instead of | ||
| /// `input.resource`. Used for `existenceCondition`. | ||
| pub(super) resource_override_reg: Option<u8>, | ||
|
|
||
| // -- Metadata accumulators --------------------------------------------- | ||
| pub(super) observed_field_kinds: BTreeSet<String>, | ||
| pub(super) observed_aliases: BTreeSet<String>, | ||
| pub(super) observed_tag_names: BTreeSet<String>, | ||
| pub(super) observed_operators: BTreeSet<String>, | ||
| pub(super) observed_resource_types: BTreeSet<String>, | ||
| pub(super) observed_uses_count: bool, | ||
| pub(super) observed_has_dynamic_fields: bool, | ||
| pub(super) observed_has_wildcard_aliases: bool, | ||
|
|
||
| /// When `true`, unknown aliases are silently treated as raw property paths. | ||
| pub(super) alias_fallback_to_raw: bool, | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Core infrastructure | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| impl Compiler { | ||
| pub(super) fn new() -> Self { | ||
| Self { | ||
| register_counter: 0, | ||
| ..Self::default() | ||
| } | ||
| } | ||
|
|
||
| pub(super) fn compile(mut self, rule: &PolicyRule) -> Result<Rc<Program>> { | ||
| let cond_reg = self.compile_constraint(&rule.condition)?; | ||
| self.emit( | ||
| Instruction::ReturnUndefinedIfNotTrue { | ||
| condition: cond_reg, | ||
| }, | ||
| &rule.span, | ||
| ); | ||
|
|
||
| let effect_reg = self.compile_effect(rule)?; | ||
| self.emit( | ||
| Instruction::Return { value: effect_reg }, | ||
| &rule.then_block.span, | ||
| ); | ||
|
|
||
| self.program.main_entry_point = 0; | ||
| self.program.entry_points.insert("main".to_string(), 0); | ||
| self.program.dispatch_window_size = self.register_high_water.max(2); | ||
| self.program.max_rule_window_size = 0; | ||
|
|
||
| if !self.program.builtin_info_table.is_empty() { | ||
| self.program.initialize_resolved_builtins()?; | ||
| } | ||
|
|
||
| self.program | ||
| .validate_limits() | ||
| .map_err(|message| anyhow!(message))?; | ||
|
|
||
| self.populate_compiled_annotations(); | ||
|
|
||
| Ok(Rc::new(self.program)) | ||
| } | ||
|
|
||
| // -- register / span / emit helpers ------------------------------------ | ||
|
|
||
| /// Restore `register_counter` to `saved` while protecting cached registers. | ||
| pub(super) fn restore_register_counter(&mut self, saved: u8) { | ||
| let mut floor = saved; | ||
| if let Some(r) = self.cached_input_reg { | ||
| floor = floor.max(r.saturating_add(1)); | ||
| } | ||
| if let Some(r) = self.cached_context_reg { | ||
| floor = floor.max(r.saturating_add(1)); | ||
| } | ||
| self.register_counter = floor; | ||
| } | ||
|
|
||
| pub(super) fn alloc_register(&mut self) -> Result<u8> { | ||
| if self.register_counter == u8::MAX { | ||
| bail!("azure-policy compiler exhausted RVM registers"); | ||
| } | ||
| let reg = self.register_counter; | ||
| self.register_counter = self.register_counter.saturating_add(1); | ||
| if self.register_counter > self.register_high_water { | ||
| self.register_high_water = self.register_counter; | ||
| } | ||
| Ok(reg) | ||
| } | ||
|
|
||
| pub(super) fn span_info(&mut self, span: &crate::lexer::Span) -> SpanInfo { | ||
| let path = span.source.get_path().to_string(); | ||
| let source_index = if let Some(index) = self.source_to_index.get(path.as_str()) { | ||
| *index | ||
| } else { | ||
| let index = self | ||
| .program | ||
| .add_source(path.clone(), span.source.get_contents().to_string()); | ||
| self.source_to_index.insert(path, index); | ||
| index | ||
| }; | ||
|
|
||
| SpanInfo::from_lexer_span(span, source_index) | ||
| } | ||
|
|
||
| pub(super) fn emit(&mut self, instruction: Instruction, span: &crate::lexer::Span) { | ||
| let span_info = self.span_info(span); | ||
| self.program.add_instruction(instruction, Some(span_info)); | ||
| } | ||
|
|
||
| /// Return the PC (instruction index) that the *next* emitted instruction | ||
| /// will occupy. | ||
| pub(super) fn current_pc(&self) -> Result<u16> { | ||
| u16::try_from(self.program.instructions.len()) | ||
| .map_err(|_| anyhow!("instruction index overflow")) | ||
| } | ||
|
|
||
| /// Patch tracked instruction indices, setting their `end_pc` field. | ||
| pub(super) fn patch_end_pc(&mut self, pcs: &[u16], end_pc: u16) -> Result<()> { | ||
| for &pc in pcs { | ||
| let idx = usize::from(pc); | ||
| let instr = self | ||
| .program | ||
| .instructions | ||
| .get_mut(idx) | ||
| .ok_or_else(|| anyhow!("patch_end_pc: pc {} out of bounds", pc))?; | ||
| match instr { | ||
| Instruction::LogicalBlockStart { | ||
| end_pc: ref mut ep, .. | ||
| } | ||
| | Instruction::AllOfNext { | ||
| end_pc: ref mut ep, .. | ||
| } | ||
| | Instruction::AnyOfNext { | ||
| end_pc: ref mut ep, .. | ||
| } => { | ||
| *ep = end_pc; | ||
| } | ||
| _ => { | ||
| bail!("patch_end_pc: unexpected instruction at pc {}", pc); | ||
| } | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
anakrish marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| #![allow(dead_code)] | ||
|
|
||
| //! `count` / `count.where` loop compilation. | ||
| //! | ||
| //! Stub — real implementation added in a later commit. | ||
|
|
||
| use anyhow::{bail, Result}; | ||
|
|
||
| use crate::languages::azure_policy::ast::{Condition, CountNode}; | ||
|
|
||
| use super::core::Compiler; | ||
|
|
||
| impl Compiler { | ||
| pub(super) fn compile_count(&mut self, _count_node: &CountNode) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("count compilation not yet implemented") | ||
| } | ||
|
|
||
| pub(super) const fn try_compile_count_as_any( | ||
| &mut self, | ||
| _count_node: &CountNode, | ||
| _condition: &Condition, | ||
| ) -> Result<Option<u8>> { | ||
| _ = self.register_counter; | ||
| Ok(None) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Existence-pattern optimization (count → Any loop). | ||
| //! | ||
| //! Stub — real implementation added in a later commit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| #![allow(dead_code)] | ||
|
|
||
| //! Count-binding resolution and `current()` references. | ||
| //! | ||
| //! Stub — real implementation added in a later commit. | ||
|
|
||
| use anyhow::{bail, Result}; | ||
|
|
||
| use super::core::{Compiler, CountBinding}; | ||
|
|
||
| impl Compiler { | ||
| pub(super) const fn resolve_count_binding( | ||
| &self, | ||
| _field_path: &str, | ||
| ) -> Result<Option<CountBinding>> { | ||
| _ = self.register_counter; | ||
| Ok(None) | ||
| } | ||
|
|
||
| pub(super) fn compile_from_binding( | ||
| &mut self, | ||
| _binding: CountBinding, | ||
| _field_path: &str, | ||
| _span: &crate::lexer::Span, | ||
| ) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("count binding compilation not yet implemented") | ||
| } | ||
|
|
||
| pub(super) fn compile_current_reference( | ||
| &mut self, | ||
| _key: &str, | ||
| _span: &crate::lexer::Span, | ||
| ) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("current() reference not yet implemented") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| #![allow(dead_code)] | ||
|
|
||
| //! Effect compilation (dispatch + cross-resource). | ||
| //! | ||
| //! Stub — real implementation added in a later commit. | ||
|
|
||
| use anyhow::{bail, Result}; | ||
|
|
||
| use crate::languages::azure_policy::ast::PolicyRule; | ||
|
|
||
| use super::core::Compiler; | ||
|
|
||
| impl Compiler { | ||
| pub(super) fn compile_effect(&mut self, _rule: &PolicyRule) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("effect compilation not yet implemented") | ||
| } | ||
|
|
||
| pub(super) fn wrap_effect_result( | ||
| &mut self, | ||
| _effect_name_reg: u8, | ||
| _details_reg: Option<u8>, | ||
| _span: &crate::lexer::Span, | ||
| ) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("wrap_effect_result not yet implemented") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Modify / Append effect detail compilation. | ||
| //! | ||
| //! Stub — real implementation added in a later commit. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
| #![allow(dead_code)] | ||
|
|
||
| //! Template-expression and call-expression compilation. | ||
| //! | ||
| //! Stub — real implementation added in a later commit. | ||
|
|
||
| use anyhow::{bail, Result}; | ||
|
|
||
| use crate::languages::azure_policy::ast::{Expr, JsonValue, ValueOrExpr}; | ||
|
|
||
| use super::core::Compiler; | ||
|
|
||
| impl Compiler { | ||
| pub(super) fn compile_value_or_expr( | ||
| &mut self, | ||
| _voe: &ValueOrExpr, | ||
| _span: &crate::lexer::Span, | ||
| ) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("expression compilation not yet implemented") | ||
| } | ||
|
|
||
| pub(super) fn compile_json_value( | ||
| &mut self, | ||
| _value: &JsonValue, | ||
| _span: &crate::lexer::Span, | ||
| ) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("JSON value compilation not yet implemented") | ||
| } | ||
|
|
||
| pub(super) fn compile_expr(&mut self, _expr: &Expr) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("expression compilation not yet implemented") | ||
| } | ||
|
|
||
| pub(super) fn compile_call_expr( | ||
| &mut self, | ||
| _span: &crate::lexer::Span, | ||
| _func: &str, | ||
| _args: &[Expr], | ||
| ) -> Result<u8> { | ||
| let _ = self; | ||
| bail!("call expression compilation not yet implemented") | ||
| } | ||
|
|
||
| pub(super) fn compile_call_args(&mut self, _args: &[Expr]) -> Result<alloc::vec::Vec<u8>> { | ||
| let _ = self; | ||
| bail!("call args compilation not yet implemented") | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.