Skip to content

Commit 2242ebc

Browse files
committed
Phase B5: Add lir_types_c.h with C-visible LIR struct layouts
Create C-compatible struct definitions for BasicBlock (LirBasicBlock) and Function (LirFunction) that mirror the C++ struct memory layouts exactly. C files can include this header to access struct fields directly without C++ headers. Instruction and OperandBase remain opaque (accessed via lir_c_api.h) until their C++ internals (std::variant, virtual destructor) are converted. This header unlocks conversion of LIR implementation files from .cpp to .c by providing C-visible type definitions for the two simplest core types.
1 parent 37635ba commit 2242ebc

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

Python/jit/lir/lir_types_c.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* lir_types_c.h -- C-compatible struct definitions for LIR core types
3+
*
4+
* Phase B5: Provides C-visible struct layouts that match the C++ struct
5+
* memory layout exactly. C files include this header to access LIR struct
6+
* fields directly, without going through the C++ headers.
7+
*
8+
* IMPORTANT: Field names, types, and ORDER must match the corresponding
9+
* C++ struct definitions in block.h and function.h exactly. Any mismatch
10+
* causes undefined behavior when C code accesses objects created by C++.
11+
*
12+
* Currently covers: LirBasicBlock (block.h), LirFunction (function.h).
13+
* Instruction and OperandBase are accessed via opaque pointers (void*)
14+
* through lir_c_api.h until their C++ internals are converted.
15+
*/
16+
17+
#ifndef JIT_LIR_TYPES_C_H
18+
#define JIT_LIR_TYPES_C_H
19+
20+
#include <stddef.h>
21+
#include <stdint.h>
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/* Forward declarations for types that remain opaque in C */
28+
typedef struct LirInstruction LirInstruction;
29+
30+
/* CodeSection enum — must match codegen::CodeSection in code_section.h */
31+
typedef enum {
32+
LIR_SECTION_HOT = 0,
33+
LIR_SECTION_COLD = 1
34+
} LirCodeSection;
35+
36+
/*
37+
* LirBasicBlock — C-visible layout of jit::lir::BasicBlock (block.h)
38+
*
39+
* Field order MUST match block.h lines 175-191 exactly.
40+
* No vtable (no virtual methods).
41+
*/
42+
typedef struct LirBasicBlock {
43+
int id_;
44+
struct LirFunction* func_;
45+
46+
struct LirBasicBlock** successors_;
47+
size_t num_succs_;
48+
size_t succs_capacity_;
49+
50+
struct LirBasicBlock** predecessors_;
51+
size_t num_preds_;
52+
size_t preds_capacity_;
53+
54+
/* Intrusive doubly-linked list of instructions */
55+
LirInstruction* instr_head_;
56+
LirInstruction* instr_tail_;
57+
size_t num_instrs_;
58+
59+
LirCodeSection section_;
60+
} LirBasicBlock;
61+
62+
/*
63+
* LirFunction — C-visible layout of jit::lir::Function (function.h)
64+
*
65+
* Field order MUST match function.h lines 63-74 exactly.
66+
* No vtable (no virtual methods).
67+
*
68+
* Note: hir_func_ is an opaque pointer to the HIR Function (C++ only).
69+
* C code should not dereference it.
70+
*/
71+
typedef struct LirFunction {
72+
const void* hir_func_; /* const hir::Function* — opaque in C */
73+
74+
struct LirBasicBlock** blocks_;
75+
size_t num_blocks_;
76+
size_t blocks_capacity_;
77+
78+
int next_id_;
79+
} LirFunction;
80+
81+
#ifdef __cplusplus
82+
} /* extern "C" */
83+
#endif
84+
85+
#endif /* JIT_LIR_TYPES_C_H */

0 commit comments

Comments
 (0)