Allow assignments of runtime values#507
Open
daniellerozenblit wants to merge 10 commits intofacebook:devfrom
Open
Allow assignments of runtime values#507daniellerozenblit wants to merge 10 commits intofacebook:devfrom
daniellerozenblit wants to merge 10 commits intofacebook:devfrom
Conversation
Summary: Pull Request resolved: facebook#486 # Stack The goal of this stack is to add support for Parameterized Records to sddl2. # Diff This diff adds parsing and semantic analysis support for parameterized records in SDDL2. Parameterized records allow record types to accept parameters that can be used within field definitions, enabling more flexible type definitions. ## Key Changes - **ASTCall node:** New AST node type representing a parameterized record instantiation (e.g., `Entry(N)`). Contains a target (the record type) and arguments. - **CallRule grammar:** New grammar rule that parses parenthesized argument lists after identifiers as function calls, enabling syntax like `Foo(3, 5)`. - **SemanticAnalyzer updates:** - Extended `analyze(ASTRecord)` to validate record parameters are variable names and introduce them as NUMERIC types within the record scope. - Added `analyze(ASTCall)` to validate the target resolves to a record type, argument count matches parameter count, and all arguments are numeric. - Updated `analyzeMember()` to add record params to scope when accessing fields. - **Optimizer passes:** Extended ConstFoldPass and DeadVarPass to handle the new ASTCall node type. - **CodeGenerator:** Added stub for CALL handling (implementation in next diff). Differential Revision: D95827616
Summary: Pull Request resolved: facebook#487 # Stack The goal of this stack is to add support for Parameterized Records to sddl2. # Diff This diff adds code generation support for parameterized records in SDDL2, allowing records to accept parameters that can be used in field definitions. ## Key Changes - **bindParams():** New helper function that binds record parameters to registers before generating record body code. Parameters are temporarily assigned to registers so field expressions can reference them. - **generateType() for CALL:** Added handling for ConvertedNodeType::CALL which resolves the target record type, binds parameters, generates the record body, then restores registers. - **generateMember():** Extended to handle parameterized records by detecting CALL nodes and binding parameters before walking through record fields. - **generateAssign():** Modified to detect parameterized records (records with non-empty params) and store them as type aliases without generating code, deferring code generation until instantiation. - **generateAssume():** Extended to support CALL nodes alongside records for assume operations. Differential Revision: D95827634
Summary: ## Stack The goal of this stack is to add support for conditional `when` blocks to sddl. ## Diff Refactor `ConstFoldPass` and `DeadVarPass` optimizer implementations to improve code organization, reduce duplication, and establish consistent naming conventions. This is a pure refactor with no behavioral changes—prepares the optimizer infrastructure for upcoming `when` block support. Differential Revision: D96342197
Summary:
## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.
## Diff
This diff adds parsing and semantic analysis support for `when` blocks in sddl..
### Changes
- **ASTWhen node:** New AST node type representing a conditional `when` block. Contains a condition expression and a body of statements.
- **WhenRule grammar:** New grammar rule that parses `when <expr> { <statements> }` syntax.
- **SemanticAnalyzer:** Added `analyze(ASTWhen)` to validate the condition is numeric and recursively analyze the body statements.
Differential Revision: D96342217
Summary:
## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.
## Diff
This diff implements optimizer passes for `when` blocks in sddl., enabling constant folding and dead variable elimination for conditional constructs.
### Changes
- **ConstFoldPass:** Implemented `optimizeWhen()` that folds constant conditions—eliminates `when 0 { ... }` blocks entirely (dead code removal) and inlines the body for `when <non-zero> { ... }`.
- **DeadVarPass:** Implemented `optimizeWhen()` that tracks variable references in condition and body, applies dead variable elimination to when body.
Differential Revision: D96342234
Summary: ## Stack The goal of this stack is to add support for conditional `when` blocks to sddl. ## Diff This diff adds a new VM opcode `jump_if` that conditionally skips N instructions based on a condition. This opcode is required for implementing `when` blocks. Differential Revision: D96342363
Summary: ## Stack The goal of this stack is to add support for conditional `when` blocks to sddl. ## Diff Add semantic analysis check to prevent member access (e.g., `data.optional`) on fields declared inside `when` blocks. Conditional fields may or may not exist at runtime depending on the `when` condition, so allowing direct member access would lead to undefined behavior. Differential Revision: D96347373
Summary: Pull Request resolved: facebook#503 ## Stack The goal of this stack is to add support for conditional `when` blocks to sddl. ## Diff Introduce a dedicated `ASTRecordField` type to explicitly represent record fields instead of using `ASSUME` operations. Previously, record fields were represented as `ASSUME` ops (e.g., `field_name assume Type`), which made it difficult to distinguish field declarations from other operations. This change creates a proper AST node type for record fields, improving code clarity and enabling better support for conditional fields in subsequent diffs. Differential Revision: D96342377
Summary: Pull Request resolved: facebook#501 ## Stack The goal of this stack is to add support for conditional `when` blocks to sddl. ## Diff Implement code generation for `when` blocks, enabling conditional field consumption based on runtime expressions. This diff completes the `when` block feature by adding the code generation phase. Previous diffs in the stack added parsing, semantic analysis, optimizer support, and the `jump_if` VM opcode—this ties them all together. ### Changes - **generateWhen():** New method that generates code for `when` blocks using the `jump_if` opcode. Emits the body instruction count, evaluates the condition, negates it, and conditionally skips the body. - **generateBlock():** Extracted block generation logic from `generate()` to enable reuse for `when` block bodies and record fields. Differential Revision: D96342427
Summary: ## Diff Allow assignment of runtime expression values to variables, enabling syntax like `sum = x + y` where the RHS is a runtime computation rather than a type definition. Previously, all variable assignments expected a type on the right-hand side (e.g., `Foo = Int32LE`). This change extends the code generator to handle assignments where the RHS evaluates to a runtime numeric value by catching the `InvariantViolation` thrown when type generation fails and falling back to value generation. Differential Revision: D96343046
|
@daniellerozenblit has exported this pull request. If you are a Meta employee, you can view the originating Diff in D96343046. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
Diff
Allow assignment of runtime expression values to variables, enabling syntax like
sum = x + ywhere the RHS is a runtime computation rather than a type definition.Previously, all variable assignments expected a type on the right-hand side (e.g.,
Foo = Int32LE). This change extends the code generator to handle assignments where the RHS evaluates to a runtime numeric value by catching theInvariantViolationthrown when type generation fails and falling back to value generation.Differential Revision: D96343046