-
Notifications
You must be signed in to change notification settings - Fork 23
Open
Description
Description
Create a way to instantiate structs with initial field values using a new StructExpr expression class.
Tasks
- Create
StructExprclass insrc/irx/system.py:- Store
struct_type: StructTypereference - Store
field_values: Dict[str, astx.Expr]for field initializers - Generate unique name for the instance
- Store
- Implement
visit(node: StructExpr)inllvmliteir.py:- Allocate struct on stack
- For each field in struct definition:
- Get value from
field_valuesor use default - Use GEP to get field pointer
- Store value to field
- Get value from
- Return pointer to struct
Example Usage
point_type = StructType(struct_name="Point")
point_expr = StructExpr(
struct_type=point_type,
field_values={
"x": astx.LiteralInt32(10),
"y": astx.LiteralInt32(20),
}
)
# Use in variable declaration
point_var = astx.VariableDeclaration(
name="p",
type_=point_type,
value=point_expr
)Expected LLVM IR Output
%struct_inst_0 = alloca %"Point"
%struct_inst_0.x_ptr = getelementptr %"Point", %"Point"* %struct_inst_0, i32 0, i32 0
store i32 10, i32* %struct_inst_0.x_ptr
%struct_inst_0.y_ptr = getelementptr %"Point", %"Point"* %struct_inst_0, i32 0, i32 1
store i32 20, i32* %struct_inst_0.y_ptrFiles to Modify
src/irx/system.py- AddStructExprclasssrc/irx/builders/llvmliteir.py- AddStructExprvisitor
Dependencies
- Requires (struct type definition) Add LLVM IR codegen for
StructDefStmt(struct type definitions) #143 - Requires (struct type reference) Add
StructTypefor referencing defined structs in variable declarations #152
Acceptance Criteria
-
StructExprcan create struct instances with field values - Default values from struct definition are used when not provided
- Generated LLVM IR correctly initializes all fields
- Unit test
test_struct_instantiationpasses
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels