-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add StructDefStmt LLVM IR codegen #150
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -155,6 +155,8 @@ class LLVMLiteIRVisitor(BuilderVisitor): | |||||||||||||||||||||
| _llvm: VariablesLLVM | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| function_protos: dict[str, astx.FunctionPrototype] | ||||||||||||||||||||||
| struct_types: dict[str, ir.IdentifiedStructType] | ||||||||||||||||||||||
| struct_defs: dict[str, astx.StructDefStmt] | ||||||||||||||||||||||
| result_stack: list[ir.Value | ir.Function] = [] | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def __init__(self) -> None: | ||||||||||||||||||||||
|
|
@@ -164,6 +166,8 @@ def __init__(self) -> None: | |||||||||||||||||||||
| # named_values as instance variable so it isn't shared across instances | ||||||||||||||||||||||
| self.named_values: dict[str, Any] = {} | ||||||||||||||||||||||
| self.function_protos: dict[str, astx.FunctionPrototype] = {} | ||||||||||||||||||||||
| self.struct_types: dict[str, ir.IdentifiedStructType] = {} | ||||||||||||||||||||||
| self.struct_defs: dict[str, astx.StructDefStmt] = {} | ||||||||||||||||||||||
| self.result_stack: list[ir.Value | ir.Function] = [] | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| self.initialize() | ||||||||||||||||||||||
|
|
@@ -1914,6 +1918,24 @@ def visit(self, node: astx.FunctionDef) -> None: | |||||||||||||||||||||
| self.visit(node.body) | ||||||||||||||||||||||
| self.result_stack.append(fn) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @dispatch # type: ignore[no-redef] | ||||||||||||||||||||||
| def visit(self, node: astx.StructDefStmt) -> None: | ||||||||||||||||||||||
| """Translate ASTx StructDefStmt to LLVM-IR.""" | ||||||||||||||||||||||
| if node.name in self.struct_types: | ||||||||||||||||||||||
| raise Exception(f"Struct '{node.name}' already defined.") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| struct_type = self._llvm.module.context.get_identified_type(node.name) | ||||||||||||||||||||||
| self.struct_types[node.name] = struct_type | ||||||||||||||||||||||
| self.struct_defs[node.name] = node | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| field_types = [] | ||||||||||||||||||||||
| for attr in node.attributes: | ||||||||||||||||||||||
| type_str = attr.type_.__class__.__name__.lower() | ||||||||||||||||||||||
| field_type = self._llvm.get_data_type(type_str) | ||||||||||||||||||||||
|
||||||||||||||||||||||
| field_type = self._llvm.get_data_type(type_str) | |
| try: | |
| field_type = self._llvm.get_data_type(type_str) | |
| except Exception as e: | |
| # Provide more context for unsupported struct field types. | |
| field_name = getattr(attr, "name", "<unknown>") | |
| raise Exception( | |
| f"Unsupported type '{type_str}' for field '{field_name}' " | |
| f"in struct '{node.name}'." | |
| ) from e |
Copilot
AI
Dec 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation doesn't handle the case where node.attributes is empty. Creating a struct with no fields will result in calling struct_type.set_body() with no arguments, which may not be the intended behavior for LLVM IR. Consider adding a check to handle empty structs or document this behavior if it's intentional.
| struct_type.set_body(*field_types) | |
| if field_types: | |
| # Struct with one or more fields. | |
| struct_type.set_body(*field_types) | |
| else: | |
| # Intentionally define an empty struct when there are no attributes. | |
| struct_type.set_body() |
Copilot
AI
Dec 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The visit method for StructDefStmt does not append to result_stack. Other similar visitor methods (like visit for FunctionDef at line 1919 and FunctionPrototype at line 1958) append their results to result_stack. This could cause issues if code expects to retrieve the struct type from the result stack. Consider appending struct_type to result_stack for consistency.
| struct_type.set_body(*field_types) | |
| struct_type.set_body(*field_types) | |
| self.result_stack.append(struct_type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message format is inconsistent with other error messages in the codebase. Most error messages use a prefix like "[EE]:" (see line 145) or "codegen:" (see lines 500, 507, 512, etc.). Consider using a consistent prefix format such as "codegen: Struct 'name' already defined." for better error tracking and consistency.