✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
"""
|
|
|
|
|
Call instruction lowering
|
|
|
|
|
Handles regular function calls (not BoxCall or ExternCall)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import llvmlite.ir as ir
|
|
|
|
|
from typing import Dict, List, Optional
|
|
|
|
|
|
|
|
|
|
def lower_call(
|
|
|
|
|
builder: ir.IRBuilder,
|
|
|
|
|
module: ir.Module,
|
|
|
|
|
func_name: str,
|
|
|
|
|
args: List[int],
|
|
|
|
|
dst_vid: Optional[int],
|
|
|
|
|
vmap: Dict[int, ir.Value],
|
2025-09-13 15:37:58 +09:00
|
|
|
resolver=None,
|
|
|
|
|
preds=None,
|
|
|
|
|
block_end_values=None,
|
|
|
|
|
bb_map=None
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Lower MIR Call instruction
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
builder: Current LLVM IR builder
|
|
|
|
|
module: LLVM module
|
|
|
|
|
func_name: Function name to call
|
|
|
|
|
args: List of argument value IDs
|
|
|
|
|
dst_vid: Optional destination for return value
|
|
|
|
|
vmap: Value map
|
|
|
|
|
resolver: Optional resolver for type handling
|
|
|
|
|
"""
|
2025-09-13 15:37:58 +09:00
|
|
|
# Resolve function: accepts string name or value-id referencing a string literal
|
|
|
|
|
actual_name = func_name
|
|
|
|
|
if not isinstance(func_name, str):
|
|
|
|
|
# Try resolver.string_literals
|
|
|
|
|
if resolver is not None and hasattr(resolver, 'string_literals'):
|
|
|
|
|
actual_name = resolver.string_literals.get(func_name)
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
# Look up function in module
|
|
|
|
|
func = None
|
2025-09-13 15:37:58 +09:00
|
|
|
if isinstance(actual_name, str):
|
|
|
|
|
for f in module.functions:
|
|
|
|
|
if f.name == actual_name:
|
|
|
|
|
func = f
|
|
|
|
|
break
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
|
|
|
|
|
if not func:
|
2025-09-13 15:37:58 +09:00
|
|
|
# Function not found - create declaration with default i64 signature
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
ret_type = ir.IntType(64)
|
|
|
|
|
arg_types = [ir.IntType(64)] * len(args)
|
2025-09-13 15:37:58 +09:00
|
|
|
name = actual_name if isinstance(actual_name, str) else "unknown_fn"
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
func_type = ir.FunctionType(ret_type, arg_types)
|
2025-09-13 15:37:58 +09:00
|
|
|
func = ir.Function(module, func_type, name=name)
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
|
|
|
|
|
# Prepare arguments
|
|
|
|
|
call_args = []
|
|
|
|
|
for i, arg_id in enumerate(args):
|
2025-09-13 15:37:58 +09:00
|
|
|
arg_val = None
|
|
|
|
|
if i < len(func.args):
|
|
|
|
|
expected_type = func.args[i].type
|
|
|
|
|
if resolver is not None and preds is not None and block_end_values is not None and bb_map is not None:
|
|
|
|
|
if hasattr(expected_type, 'is_pointer') and expected_type.is_pointer:
|
|
|
|
|
arg_val = resolver.resolve_ptr(arg_id, builder.block, preds, block_end_values, vmap)
|
|
|
|
|
else:
|
|
|
|
|
arg_val = resolver.resolve_i64(arg_id, builder.block, preds, block_end_values, vmap, bb_map)
|
|
|
|
|
if arg_val is None:
|
|
|
|
|
arg_val = vmap.get(arg_id)
|
|
|
|
|
if arg_val is None:
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
if i < len(func.args):
|
|
|
|
|
expected_type = func.args[i].type
|
|
|
|
|
else:
|
|
|
|
|
expected_type = ir.IntType(64)
|
|
|
|
|
if isinstance(expected_type, ir.IntType):
|
|
|
|
|
arg_val = ir.Constant(expected_type, 0)
|
|
|
|
|
elif isinstance(expected_type, ir.DoubleType):
|
|
|
|
|
arg_val = ir.Constant(expected_type, 0.0)
|
|
|
|
|
else:
|
|
|
|
|
arg_val = ir.Constant(expected_type, None)
|
|
|
|
|
if i < len(func.args):
|
|
|
|
|
expected_type = func.args[i].type
|
|
|
|
|
if hasattr(arg_val, 'type') and arg_val.type != expected_type:
|
|
|
|
|
if expected_type.is_pointer and isinstance(arg_val.type, ir.IntType):
|
2025-09-13 15:37:58 +09:00
|
|
|
arg_val = builder.inttoptr(arg_val, expected_type, name=f"call_i2p_{i}")
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
elif isinstance(expected_type, ir.IntType) and arg_val.type.is_pointer:
|
2025-09-13 15:37:58 +09:00
|
|
|
arg_val = builder.ptrtoint(arg_val, expected_type, name=f"call_p2i_{i}")
|
✨ Python LLVM backend implementation (experimental)
- Created llvmlite-based LLVM backend in src/llvm_py/
- Implemented all MIR14 instructions (const, binop, jump, branch, ret, compare, phi, call, boxcall, externcall, typeop, newbox, safepoint, barrier)
- Experimental LoopForm support
- ~2000 lines of clean Python code vs complex Rust/inkwell
- Useful for PHI/SSA validation and rapid prototyping
- Added documentation to CLAUDE.md
This was created while waiting for ChatGPT's investigation of BuilderCursor issues.
2025-09-12 20:55:13 +09:00
|
|
|
call_args.append(arg_val)
|
|
|
|
|
|
|
|
|
|
# Make the call
|
|
|
|
|
result = builder.call(func, call_args, name=f"call_{func_name}")
|
|
|
|
|
|
|
|
|
|
# Store result if needed
|
|
|
|
|
if dst_vid is not None:
|
2025-09-13 15:37:58 +09:00
|
|
|
vmap[dst_vid] = result
|